【发布时间】:2018-06-08 18:50:47
【问题描述】:
我使用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
我的代码如下所示:
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'))
但是,我想添加一个关于如何使用 -t/--template 的详细示例,例如(在 example 部分添加):
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
我不知道应该使用哪个属性来添加“示例”部分,我检查了Print program usage example with argparse modulen,但是当我检查epilog in the official doc时不清楚并且没有详细的示例。
谁能给我一个例子来说明如何做到这一点?谢谢
【问题讨论】:
-
您是否尝试过使用提到的
epilog?它有什么问题? -
是的,但是里面怎么换行,我想多行解释,然后不知道有没有更好的办法
-
您是否尝试过将
"""multiline string"""传递给epilog?我猜会这样做,其中的换行符将被保留。你可以保持整洁,就像我展示的那样here。