Python 一个参数有两个值的Argparse
现在,我的脚本通过以下方式调用:Python 一个参数有两个值的Argparse,python,command-line,argparse,Python,Command Line,Argparse,现在,我的脚本通过以下方式调用: python resylter.py -n *newfile* -o *oldfile* 代码如下所示: parser.add_argument('-n', '--newfile', help='Uses only with -o argument. Compares inputed OLD (-o) file with previous run results with NEW(-n) output.xml file with actual run resu
python resylter.py -n *newfile* -o *oldfile*
代码如下所示:
parser.add_argument('-n', '--newfile', help='Uses only with -o argument. Compares inputed OLD (-o) file with previous run results with NEW(-n) output.xml file with actual run results')
parser.add_argument('-o', '--oldfile', help='Uses only with -n argument. Compares inputed OLD (-o) file with previous run results with NEW(-n) output.xml file with actual run results')
还有一些行动
我如何编辑它以便像这样使用?:
python resylter.py -n *newfile* *oldfile*
sys.argv[-1]与
nargs='*'
我做了以下工作:
parser.add_argument('-c', '--compare', nargs = '*')
_newfile_ = _args_.compare[0]
_oldfile_ = _args_.compare[1]
它现在可以工作了以响应:
nargs='*'
中的星号表示零个或多个参数(如在regex中),这在本文中没有意义。您需要nargs=2
i、 e
另外,如果运行
resylter.py-h
使用参数nargs='*',
我不认为这是否有用,那么添加metavar=('newfile','oldfile')
可以改进帮助文本。我将oldfile
保留为(即parser.add_参数('oldfile',…)
)
parser.add_argument(
'-c',
'--compare',
nargs=2,
metavar=('newfile', 'oldfile'),
help='Compares previous run results in oldfile with actual run results in newfile.',
)
args = parser.parse_args()
newfile, oldfile = args.compare