2016-01-25 44 views
2

现在我的脚本调用通过: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 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] di dn't可与nargs = '*'

+0

使用参数'nargs ='*'' –

+0

我不认为这会有用。我只是把'oldfile'作为[位置参数](https://docs.python.org/3/library/argparse.html#name-or-flags)(即'parser.add_argument('oldfile',. ..)') – soon

回答

1

Wokrs我以下:

parser.add_argument('-c', '--compare', nargs = '*') 

_newfile_ = _args_.compare[0] 
_oldfile_ = _args_.compare[1] 

和现在的工作

1

nargs = '*'星意味着零个或多个参数(如在正则表达式),其在这种情况下没有意义。你想要nargs = 2

相关问题