我使用argparse来处理输入参数,并使用parser.print_help()输出以下内容:
optional arguments:
-h, --help show this help message and exit
-t TEMPLATES, --templates TEMPLATES
template names to make, should be defined as section
name in conf, and have related file in templates/
folder
-c CONFPATH, --confpath CONFPATH
configuration path for template detail info
Run Code Online (Sandbox Code Playgroud)
我的代码如下所示:
import argparse
parser = argparse.ArgumentParser(prog='base_maker', description='template maker')
parser.add_argument('-t', '--templates', help='template names to make, should be defined as section name in conf, and have related file in templates/ folder', type=str)
parser.add_argument('-c', '--confpath', help='configuration path for template detail info', type=str, default=os.path.join(basepath, 'conf/maker.conf'))
Run Code Online (Sandbox Code Playgroud)
但是,我想添加一个关于如何使用-t/ - template的详细示例,如(在示例部分中添加):
optional arguments:
-h, --help show this help message and exit
-t TEMPLATES, --templates TEMPLATES
template names to make, should be defined as section
name in conf, and have related file in templates/
folder
-c CONFPATH, --confpath CONFPATH
configuration path for template detail info
example:
python test.py -t template/test.py
python test.py -t template/test -c conf/test.conf
python test.py -t test.py
Run Code Online (Sandbox Code Playgroud)
我不知道应该使用哪个属性来添加"示例"部分,我使用argparse modulen检查打印程序用法示例,但是当我在官方文档中检查epilog时,它不清楚并且没有详细示例.
谁能给我一个关于如何实现这个目标的例子?谢谢
c3s*_*t7n 20
如果要在末尾打印示例(epilog)并保留空格/格式(formatter_class设置为RawDescriptionHelpFormatter),则需要使用ArgumentParser的epilog和formatter_class参数.
通过修改上面的示例示例:
import argparse
example_text = '''example:
python test.py -t template/test.py
python test.py -t template/test -c conf/test.conf
python test.py -t test.py'''
parser = argparse.ArgumentParser(prog='base_maker',
description='template maker',
epilog=example_text,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-t', '--templates', help='template names to make, should be defined as section name in conf, and have related file in templates/ folder', type=str)
parser.add_argument('-c', '--confpath', help='configuration path for template detail info', type=str, default=os.path.join(basepath, 'conf/maker.conf'))
Run Code Online (Sandbox Code Playgroud)