Argparse 参数生成了帮助,带有选项的“元变量" [英] Argparse argument generated help, 'metavar' with choices

查看:29
本文介绍了Argparse 参数生成了帮助,带有选项的“元变量"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用带有关键字 choices 的参数(可选和位置都存在此问题)时,生成的帮助输出会显示这些选择.

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

如果同一参数还包含 metavar 关键字,则生成的输出中将省略选项列表.

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

我的想法是在 usage 行中显示 metavar,但实际上在 'autohelp' 列出位置/可选参数详细信息时显示可用选项.

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?

我已经为自定义帮助功能启动了 argparse 包装器.也许这应该是我 TODO 列表上的另一个功能.

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

推荐答案

您可以将选项添加到帮助文本中.

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()

产生:

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

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

这篇关于Argparse 参数生成了帮助,带有选项的“元变量"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆