Argparse argument generated help, 'metavar' with choices

烂漫一生 提交于 2019-12-10 13:29:38

问题


When using an argument (optional and positional both have this problem) with the keyword choices, the generated help output shows those choices.

If that same argument also includes a metavar keyword, the list of choices is omitted from the generated output.

What I had in mind, was to show the metavar in the usage line, but actually show the available choices when the 'autohelp' lists positional/optional argument details.

Any simple fixes/workarounds?


I have already started an argparse wrapper for custom help functionality. Perhaps this should be another feature on my TODO list.


回答1:


You can add the choices to the help text.

parser=argparse.ArgumentParser()
parser.add_argument('-f',metavar="TEST",choices=('a','b','c'),
    help='choices, {%(choices)s}')
print parser.format_help()

produces:

usage: stack20328931.py [-h] [-f TEST]

optional arguments:
  -h, --help  show this help message and exit
  -f TEST     choices, {a, b, c}


来源:https://stackoverflow.com/questions/20328931/argparse-argument-generated-help-metavar-with-choices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!