2016-01-25 43 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

相關問題