没有重复 ALLCAPS 的 argparse 帮助 [英] argparse help without duplicate ALLCAPS
本文介绍了没有重复 ALLCAPS 的 argparse 帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想像默认的 -h
、--help
和 -v
一样显示我的选项的 argparse 帮助,--version
是,在选项后没有 ALLCAPS 文本,或者至少没有重复的 CAPS.
导入 argparsep = argparse.ArgumentParser("a foo bardustup")p.add_argument('-i', '--ini', help="使用备用ini文件")打印 '\n', p.parse_args()
这是我目前使用 python foobar.py -h
得到的:
用法:一个 foo bardustup [-h] [-i INI]可选参数:-h, --help 显示此帮助信息并退出-i INI, --ini INI 使用备用ini
这就是我想要的:
用法:一个 foo bardustup [-h] [-i INI]可选参数:-h, --help 显示此帮助信息并退出-i, --ini INI 使用备用 ini
这也是可以接受的:
-i, --ini 使用备用 ini
我使用的是 python 2.7.
解决方案
您可以自定义 usage
并将 metavar
分配给空字符串:
导入 argparsep = argparse.ArgumentParser("a foo bardustup", usage='%(prog)s [-h] [-i INI]')p.add_argument('-i', '--ini', help="使用备用ini文件", metavar='')p.print_help()
输出
<前>用法:a foo bardustup [-h] [-i INI]可选参数:-h, --help 显示此帮助信息并退出-i , --ini 使用备用 ini 文件I'd like to display argparse help for my options the same way the default -h
,--help
and -v
,--version
are, without the ALLCAPS text after the option, or at least without the duplicated CAPS.
import argparse
p = argparse.ArgumentParser("a foo bar dustup")
p.add_argument('-i', '--ini', help="use alternate ini file")
print '\n', p.parse_args()
This is what I currently get with 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
And this is what I want:
usage: a foo bar dustup [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i, --ini INI use alternate ini
This would be acceptable too:
-i, --ini use alternate ini
I'm using python 2.7.
解决方案
You could customize usage
and assign metavar
to an empty string:
import argparse
p = argparse.ArgumentParser("a foo bar dustup", usage='%(prog)s [-h] [-i INI]')
p.add_argument('-i', '--ini', help="use alternate ini file", metavar='')
p.print_help()
Output
usage: a foo bar dustup [-h] [-i INI] optional arguments: -h, --help show this help message and exit -i , --ini use alternate ini file
这篇关于没有重复 ALLCAPS 的 argparse 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文