argparse帮助,无需重复的ALLCAPS [英] argparse help without duplicate ALLCAPS

查看:61
本文介绍了argparse帮助,无需重复的ALLCAPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我目前通过python foobar.py -h获得的内容:

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

这就是我想要的:

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.

I'm using python 2.7.

推荐答案

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

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

输出


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

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

这篇关于argparse帮助,无需重复的ALLCAPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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