本文介绍了是否可以更改argparse参数名称的显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用argparse制作了一个命令行工具,该工具接受以下参数,
using argparse i have made a command line tool, which takes below arguments,
用法: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]]
PATCHES中显示TO,而在 [-d DIFF] 加上其他文字?
is it possible to display TO in
[-to [TO]]
PATCHES in [-p PATCHES]
and DIFF in [-d DIFF]
with a different text?
推荐答案
是的,如
argparse
文档:
Yes, as described in the
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
这篇关于是否可以更改argparse参数名称的显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!