[英]In python's argparse module, how can I disable printing subcommand choices between curly brackets?
如何禁用打印子命令选项,大括号之间的选项? 使用http://docs.python.org/dev/library/argparse.html#sub-commands上的示例,正常输出为:
usage: [-h] {foo,bar} ...
optional arguments:
-h, --help show this help message and exit
subcommands:
{foo,bar} additional help
我想要的是打印这个:
usage: [-h] {foo,bar} ...
optional arguments:
-h, --help show this help message and exit
subcommands:
只删除最后一行。
为了避免使用由数十个子命令组成的巨大的大括号列表来向我的用户发送垃圾邮件,我只需设置子命令对象的metavar
属性。 我的代码看起来像:
import argparse
parser = argparse.ArgumentParser(description='Stack Overflow example')
subs = parser.add_subparsers()
subs.metavar = 'subcommand'
sub = subs.add_parser('one', help='does something once')
sub = subs.add_parser('two', help='does something twice')
parser.parse_args()
使用单个-h
参数运行此脚本的输出是:
usage: tmp.py [-h] subcommand ...
Stack Overflow example
positional arguments:
subcommand
one does something once
two does something twice
optional arguments:
-h, --help show this help message and exit
结果并不完全是你所说的最佳理想情况,但我认为如果没有子类化argparse.ArgumentParser
并覆盖你需要调整的东西,它可能是最接近的,这将是凌乱的工作。
使用您自己的方法覆盖ArgumentParser.print_usage()以打印任何内容,无论您想要什么。 如果您要做的只是消除最后一行,请调用原始版本,捕获结果(通过将其发送到文件)并仅打印您想要的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.