控制 argparse 帮助参数列表的格式?
import argparse
parser = argparse.ArgumentParser(prog='tool')
args = [('-u', '--upf', 'ref. upf', dict(required='True')),
('-s', '--skew', 'ref. skew', {}),
('-m', '--model', 'ref. model', {})]
for args1, args2, desc, options in args:
parser.add_argument(args1, args2, help=desc, **options)
parser.print_help()
输出:
usage: capcheck [-h] -u UPF [-s SKEW] [-m MODEL]
optional arguments:
-h, --help show this help message and exit
-u UPF, --upf UPF ref. upf
-s SKEW, --skew SKEW ref. skew
-m MODEL, --model MODEL
ref. model
如何打印 ref. model 与 -m MODEL, --model MODEL
位于同一行,而不是当我使用 -h
选项运行脚本时出现在单独的行上?
import argparse
parser = argparse.ArgumentParser(prog='tool')
args = [('-u', '--upf', 'ref. upf', dict(required='True')),
('-s', '--skew', 'ref. skew', {}),
('-m', '--model', 'ref. model', {})]
for args1, args2, desc, options in args:
parser.add_argument(args1, args2, help=desc, **options)
parser.print_help()
Output:
usage: capcheck [-h] -u UPF [-s SKEW] [-m MODEL]
optional arguments:
-h, --help show this help message and exit
-u UPF, --upf UPF ref. upf
-s SKEW, --skew SKEW ref. skew
-m MODEL, --model MODEL
ref. model
How do I print ref. model in the same line as -m MODEL, --model MODEL
instead of that appearing on a separate line when I run the script with -h
option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于 argparse 库尝试使用 COLUMNS 环境变量来获取终端宽度,因此我们也可以设置此变量,并让 argparse 完成其工作。
在 RHEL/Python 2.7.5 上进行测试和批准
归功于 https://stackoverflow.com/a/943921 以获得真正的终端宽度
As the argparse library tries to use the
COLUMNS
environment variable to get the terminal width, we also can set this variable, and let argparse do its job.Tested and approved on RHEL/Python 2.7.5
Credits to https://stackoverflow.com/a/943921 for getting the real terminal width
另一种方法:劫持 sys.argv,检查它的 --help 和 -h,如果发现使用 argparse.format_help 提取帮助文本,对其进行处理,打印它,然后退出。
不带格式帮助:
带格式帮助:
Another approach: hijack sys.argv, check it for --help and -h, if found extract help text using argparse.format_help, massage it, print it, and exit.
Help without formatting:
with formatting:
您可以提供
formatter_class
参数:注意:
argparse.HelpFormatter
的实现是私有的,只有名称是公共的。因此,该代码可能会在argparse
的未来版本中停止工作。提交功能请求,为 http://bugs 上的max_help_position
自定义提供公共接口。 python.org/输出
You could supply
formatter_class
argument:Note: Implementation of
argparse.HelpFormatter
is private only the name is public. Therefore the code might stop working in future versions ofargparse
. File a feature request to provide a public interface for the customization ofmax_help_position
on http://bugs.python.org/Output
受到@jfs答案的启发,我提出了这个解决方案:
有了这个,您可以使用您喜欢的任何
HelpFormatter
来调用它:或者
这样做是确保实际上可以创建更宽的格式化程序使用
width
和max_help_position
参数。如果私有 API 发生变化,make_wide
会通过TypeError
进行记录,并且格式化程序将原样返回。这应该会使代码对于已部署的应用程序更加可靠。我欢迎任何让这变得更加Pythonic的建议。
Inspired by @jfs's answer, I have come up with this solution:
Having that, you can call it with any
HelpFormatter
that you like:or
What this does is make sure that the wider formatter can actually be created using the
width
andmax_help_position
arguments. If the private API changes, that is noted bymake_wide
by aTypeError
and the formatter is returned unchanged. That should make the code more reliable for deployed applications.I'd welcome any suggestions to make this more pythonic.
如果您向
ArgumentParser
提供自定义formatter_class
,然后使用子解析器,则格式化程序将仅适用于顶级帮助消息。为了对所有子解析器使用相同(或其他)格式化程序,您需要为每个
add_parser
调用提供formatter_class
参数:If you are providing a custom
formatter_class
to yourArgumentParser
and then use subparsers, the formatter will only apply to the top-level help message. In order to use the same (or some other) formatter for all subparsers, you need to provide
formatter_class
argument for eachadd_parser
call: