gpt4 book ai didi

python - 没有重复 ALLCAPS 的 argparse 帮助

转载 作者:IT老高 更新时间:2023-10-28 21:12:32 27 4
gpt4 key购买 nike

我想以与默认 -h--help-v 相同的方式显示我的选项的 argparse 帮助, --version 是,选项后没有全大写文本,或者至少没有重复的大写字母。

import argparse
p = argparse.ArgumentParser("a foo bar dustup")
p.add_argument('-i', '--ini', help="use alternate ini file")
print '\n', p.parse_args()

这是我目前使用 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

这就是我想要的:

usage: a foo bar dustup [-h] [-i INI]

optional arguments:
-h, --help show this help message and exit
-i, --ini INI use alternate ini

这也是可以接受的:

  -i, --ini             use alternate ini

我使用的是 python 2.7。

最佳答案

您可以自定义 usage 并将 metavar 分配给空字符串:

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

输出

usage: a foo bar dustup [-h] [-i INI]optional arguments:  -h, --help   show this help message and exit  -i , --ini   use alternate ini file

关于python - 没有重复 ALLCAPS 的 argparse 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9642692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com