Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一个参数有两个值的Argparse_Python_Command Line_Argparse - Fatal编程技术网

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