如何在 Python click 模块生成的使用消息末尾添加多个空行?
How to add multiple blank lines to the end of the usage message produced by the Python click module?
我有一个问题有点类似于 , however I want to add additional blank lines to an epilog at the end of the output generated by click。
我有以下代码:
EPILOG='\n' + '-' * 20
class SpecialEpilog(click.Group):
def format_epilog(self, ctx, formatter):
if self.epilog:
formatter.write_paragraph()
for line in self.epilog.split('\n'):
formatter.write_text(line)
#------------------
@click.group(cls=SpecialEpilog, epilog=EPILOG, invoke_without_command=True)
def cli():
"""Wraps cloud.tenable.com Nessus API calls in useful ways
\b
The CLI provides access to these subcommands:
- agent
- os
- vuln
Each subcommand can perform useful API queries within their respective domain.
"""
pass
#------------------
# main
cli.add_command(os)
cli.add_command(agent)
cli.add_command(vuln)
这会产生以下用法输出:
Usage: nessus_query [OPTIONS] COMMAND [ARGS]...
Wraps cloud.tenable.com Nessus API calls in useful ways
The CLI provides access to these subcommands:
- agent
- os
- vuln
Each subcommand can perform useful API queries within their respective
domain.
Options:
--help Show this message and exit.
Commands:
agent API calls focusing on assets' details - Works...
os API calls focusing on operating systems -...
vuln API calls focusing on vulnerabilities - Works...
--------------------
$ myprompt>
我的问题:
我想不出不需要可打印字符的方法。如果我删除上面的破折号序列,换行符 (\n
) 将不再显示。换句话说,上面的用法是这样的:
...
Commands:
agent API calls focusing on assets' details - Works...
os API calls focusing on operating systems -...
vuln API calls focusing on vulnerabilities - Works...
$ myprompt>
问题是 click
进行了优化以删除帮助末尾的所有空行。该行为在 click.Command.get_help()
中,可以像这样被覆盖:
代码:
class SpecialEpilog(click.Group):
def get_help(self, ctx):
""" standard get help, but without rstrip """
formatter = ctx.make_formatter()
self.format_help(ctx, formatter)
return formatter.getvalue()
测试代码:
import click
EPILOG = '\n\n'
class SpecialEpilog(click.Group):
def format_epilog(self, ctx, formatter):
if self.epilog:
formatter.write_paragraph()
for line in self.epilog.split('\n'):
formatter.write_text(line)
def get_help(self, ctx):
""" standard get help, but without rstrip """
formatter = ctx.make_formatter()
self.format_help(ctx, formatter)
return formatter.getvalue()
@click.group(cls=SpecialEpilog, epilog=EPILOG, invoke_without_command=True)
def cli():
pass
@cli.command()
def os(*args, **kwargs):
pass
@cli.command()
def agent(*args, **kwargs):
pass
cli(['--help'])
但我需要的只是空白,而不是结尾:
如果您只需要一些空行,那么我们可以完全忽略结尾并简单地修改 get_help()
以添加相同的内容,例如:
class AddSomeBlanksToHelp(click.Group):
def get_help(self, ctx):
return super(AddSomeBlanksToHelp, self).get_help(ctx) + '\n\n'
@click.group(cls=AddSomeBlanksToHelp, invoke_without_command=True)
我有一个问题有点类似于
我有以下代码:
EPILOG='\n' + '-' * 20
class SpecialEpilog(click.Group):
def format_epilog(self, ctx, formatter):
if self.epilog:
formatter.write_paragraph()
for line in self.epilog.split('\n'):
formatter.write_text(line)
#------------------
@click.group(cls=SpecialEpilog, epilog=EPILOG, invoke_without_command=True)
def cli():
"""Wraps cloud.tenable.com Nessus API calls in useful ways
\b
The CLI provides access to these subcommands:
- agent
- os
- vuln
Each subcommand can perform useful API queries within their respective domain.
"""
pass
#------------------
# main
cli.add_command(os)
cli.add_command(agent)
cli.add_command(vuln)
这会产生以下用法输出:
Usage: nessus_query [OPTIONS] COMMAND [ARGS]...
Wraps cloud.tenable.com Nessus API calls in useful ways
The CLI provides access to these subcommands:
- agent
- os
- vuln
Each subcommand can perform useful API queries within their respective
domain.
Options:
--help Show this message and exit.
Commands:
agent API calls focusing on assets' details - Works...
os API calls focusing on operating systems -...
vuln API calls focusing on vulnerabilities - Works...
--------------------
$ myprompt>
我的问题:
我想不出不需要可打印字符的方法。如果我删除上面的破折号序列,换行符 (\n
) 将不再显示。换句话说,上面的用法是这样的:
...
Commands:
agent API calls focusing on assets' details - Works...
os API calls focusing on operating systems -...
vuln API calls focusing on vulnerabilities - Works...
$ myprompt>
问题是 click
进行了优化以删除帮助末尾的所有空行。该行为在 click.Command.get_help()
中,可以像这样被覆盖:
代码:
class SpecialEpilog(click.Group):
def get_help(self, ctx):
""" standard get help, but without rstrip """
formatter = ctx.make_formatter()
self.format_help(ctx, formatter)
return formatter.getvalue()
测试代码:
import click
EPILOG = '\n\n'
class SpecialEpilog(click.Group):
def format_epilog(self, ctx, formatter):
if self.epilog:
formatter.write_paragraph()
for line in self.epilog.split('\n'):
formatter.write_text(line)
def get_help(self, ctx):
""" standard get help, but without rstrip """
formatter = ctx.make_formatter()
self.format_help(ctx, formatter)
return formatter.getvalue()
@click.group(cls=SpecialEpilog, epilog=EPILOG, invoke_without_command=True)
def cli():
pass
@cli.command()
def os(*args, **kwargs):
pass
@cli.command()
def agent(*args, **kwargs):
pass
cli(['--help'])
但我需要的只是空白,而不是结尾:
如果您只需要一些空行,那么我们可以完全忽略结尾并简单地修改 get_help()
以添加相同的内容,例如:
class AddSomeBlanksToHelp(click.Group):
def get_help(self, ctx):
return super(AddSomeBlanksToHelp, self).get_help(ctx) + '\n\n'
@click.group(cls=AddSomeBlanksToHelp, invoke_without_command=True)