【发布时间】:2012-07-04 04:52:47
【问题描述】:
我想以与默认 -h、--help 和 -v、--version 相同的方式显示我的选项的 argparse 帮助,选项后没有大写文本,或者至少没有重复的文本大写字母。
import argparse
p = argparse.ArgumentParser("a foo bar dustup")
p.add_argument('-i', '--ini', help="use alternate ini file")
print '\n', p.parse_args()
这是我目前通过python foobar.py -h 得到的:
usage: a foo bar dustup [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i INI, --ini INI use alternate ini
这就是我想要的:
usage: a foo bar dustup [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i, --ini INI use alternate ini
这也是可以接受的:
-i, --ini use alternate ini
我使用的是 python 2.7。
【问题讨论】:
-
我认为我不同意这个问题的前提。命令行参数通常是从 shell 传递的,按照惯例,shell 脚本中的变量通常都是大写的。我认为区分大写的元变量和小写的关键字是合适的。
-
@TokenMacGuy 考虑到这个约定,我会接受它,除了不必要地重复 UP 案例(
-i INI, --ini INI)。我已经相应地更新了 Q 标题,因为它是更令人烦恼的重复。 -
@TokenMacGuy 作为用户,您应该可以灵活地使用任何您认为合适的帮助信息。我个人觉得 CAPS 版本很难看,更不用说重复了。即使你打破了 shell 约定,也可以区分元变量,例如
git commit -h