是否可以更改argparse参数名称的显示文本

时间:2014-02-02 10:43:34

标签: python argparse

使用argparse我已经创建了一个命令行工具,它使用了以下参数,

usage: ipush [-h] [-v] [-c] [-to [TO]] [-V] [-p PATCHES] [-d DIFF]

来自以下代码..

parser = argparse.ArgumentParser(prog='ipush',
    description='Utility to push the last commit and email the color diff')
parser.add_argument('-v', '--verbose', action='store_true',
    help='if enabled will spit every command and its resulting data.')
parser.add_argument('-c', '--compose', action='store_true',
    help='compose message in default git editor to be sent prefixed with diff')
parser.add_argument('-to', type=validate_address, default=os.getenv('myemail'),
    help='enter a valid email you want to send to.', nargs='?')
parser.add_argument('-V', '--version', action='version',
    version='%(prog)s 1.0')
parser.add_argument('-p', '--patches', type=int, default=0,
    help='total number of pathces of last commits to email')
parser.add_argument('-d', '--diff', required=False, default='HEAD^ HEAD',
    help='if present pass arguments to it as you \
    will do to git diff in inverted commas')

是否可以在[-to [TO]]中的[-p PATCHES] PATCHES和[-d DIFF]中使用不同文字的DIFF中显示TO?

1 个答案:

答案 0 :(得分:1)

是的,如argparse documentation

中所述
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', metavar='YYY')
>>> parser.add_argument('bar', metavar='XXX')
>>> parser.parse_args('X --foo Y'.split())
Namespace(bar='X', foo='Y')
>>> parser.print_help()
usage:  [-h] [--foo YYY] XXX

positional arguments:
 XXX

optional arguments:
 -h, --help  show this help message and exit
 --foo YYY