Python Argparse - 如何向默认帮助消息添加文本?
Python Argparse - How can I add text to the default help message?
我正在使用 python 的 argparse 来处理参数解析。
我收到一条结构如下的默认帮助消息:
usage: ProgramName [-h] ...
Description
positional arguments:
...
optional arguments:
-h, --help show this help message and exit
...
我想要的是在此消息中添加一个全新的部分,例如:
usage: ProgramName [-h] ...
Description
positional arguments:
...
optional arguments:
-h, --help show this help message and exit
...
additional information:
This will show additional information relevant to the user.
....
有没有办法实现这种行为?
python 2.7 和 3.x 都支持的解决方案是首选。
编辑:
我还希望有一个解决方案,可以在帮助消息的底部添加新的部分。
您可以通过多种方式add a description执行您的命令。
推荐的方法是在源代码文件的顶部添加模块文档,如下所示:
""" This is the description, it will be accessible within the variable
__doc__
"""
然后:
parser = argparse.ArgumentParser(description=__doc__)
要在参数说明下方添加文本,请使用 epilog,如以下取自文档的示例所示:
>>> parser = argparse.ArgumentParser(description='A foo that bars',
epilog="And that's how you'd foo a bar")
>>> parser.print_help()
usage: argparse.py [-h]
A foo that bars
optional arguments: -h, --help show this help message and exit
And that's how you'd foo a bar
有关详细信息,请参阅文档(上面链接)。
您完全可以使用 epilog 来完成。
下面是一个例子:
import argparse
import textwrap
parser = argparse.ArgumentParser(
prog='ProgramName',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
additional information:
I have indented it
exactly the way
I want it
'''))
parser.add_argument('--foo', nargs='?', help='foo help')
parser.add_argument('bar', nargs='+', help='bar help')
parser.print_help()
结果:
usage: ProgramName [-h] [--foo [FOO]] bar [bar ...]
positional arguments:
bar bar help
optional arguments:
-h, --help show this help message and exit
--foo [FOO] foo help
additional information:
I have indented it
exactly the way
I want it
我正在使用 python 的 argparse 来处理参数解析。 我收到一条结构如下的默认帮助消息:
usage: ProgramName [-h] ...
Description
positional arguments:
...
optional arguments:
-h, --help show this help message and exit
...
我想要的是在此消息中添加一个全新的部分,例如:
usage: ProgramName [-h] ...
Description
positional arguments:
...
optional arguments:
-h, --help show this help message and exit
...
additional information:
This will show additional information relevant to the user.
....
有没有办法实现这种行为? python 2.7 和 3.x 都支持的解决方案是首选。
编辑: 我还希望有一个解决方案,可以在帮助消息的底部添加新的部分。
您可以通过多种方式add a description执行您的命令。 推荐的方法是在源代码文件的顶部添加模块文档,如下所示:
""" This is the description, it will be accessible within the variable
__doc__
"""
然后:
parser = argparse.ArgumentParser(description=__doc__)
要在参数说明下方添加文本,请使用 epilog,如以下取自文档的示例所示:
>>> parser = argparse.ArgumentParser(description='A foo that bars',
epilog="And that's how you'd foo a bar")
>>> parser.print_help()
usage: argparse.py [-h]
A foo that bars
optional arguments: -h, --help show this help message and exit
And that's how you'd foo a bar
有关详细信息,请参阅文档(上面链接)。
您完全可以使用 epilog 来完成。 下面是一个例子:
import argparse
import textwrap
parser = argparse.ArgumentParser(
prog='ProgramName',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
additional information:
I have indented it
exactly the way
I want it
'''))
parser.add_argument('--foo', nargs='?', help='foo help')
parser.add_argument('bar', nargs='+', help='bar help')
parser.print_help()
结果:
usage: ProgramName [-h] [--foo [FOO]] bar [bar ...]
positional arguments:
bar bar help
optional arguments:
-h, --help show this help message and exit
--foo [FOO] foo help
additional information:
I have indented it
exactly the way
I want it