python argparse多个参数处理

时间:2017-04-29 01:05:01

标签: python argparse

这里的初学者。需要一些关于argparse的帮助。以下是代码。

#!/usr/bin/python

import argparse
import re
import string

p = argparse.ArgumentParser()
p.add_argument('-a', '--add', help="input the data in format ip:port:name", nargs='*') 
p.add_argument('-d', '--delete', help="input the data in format ip:port:name", nargs='*') 
args = p.parse_args()
add_List = args.add 
del_List = args.delete

addN = "adding.."
delN = "deleting.."

def test(what):
    for i in range(5):
        print what

######### expected output: "adding.." 5 times 
for i in add_List:
    test(addN)

######### expected output: "deleting.." 5 times 
for i in deL_List:
    test(delN)

我基本上想要的是当脚本运行时,

  
      
  1. ./ script -a 1.1.1.1:99:na1它应该打印“添加..”5次
  2.   
  3. ./ script -d 1.1.1.1:99:na1它应该打印“删除..”5次
  4.   

请不要介意脚本cos给出的参数,它们将单独处理。我只是想了解在这种情况下使用argparse和适当条件的最有效方法。如果我能打印出合理的使用指南,那将会很棒。

2 个答案:

答案 0 :(得分:1)

当我第一次运行你发布的代码时,我得到了

1550:~/mypy$ python stack43690807.py -a 1.1.1.1:99:na1
adding..
adding..
adding..
adding..
adding..
Traceback (most recent call last):
  File "stack43690807.py", line 23, in <module>
    for i in deL_List:
NameError: name 'deL_List' is not defined

如果我更正拼写错误(大写字母L)

1819:~/mypy$ python stack43690807.py -a 1.1.1.1:99:na1
adding..
adding..
adding..
adding..
adding..
Traceback (most recent call last):
  File "stack43690807.py", line 23, in <module>
    for i in del_List:
TypeError: 'NoneType' object is not iterable

此问题是默认值为Nonefor i in None:不起作用。所以我们要么需要测试None,例如

if add_List is not None:
   for i in add_List:
   ....

或者我们需要将默认值更改为default=[]。我还添加了print(args)语句,因此我们可以更清楚地了解解析器产生的内容。

1819:~/mypy$ python stack43690807.py -a 1.1.1.1:99:na1
Namespace(add=['1.1.1.1:99:na1'], delete=[])
adding..
adding..
adding..
adding..
adding..
1820:~/mypy$ python stack43690807.py -d 1.1.1.1:99:na1
Namespace(add=[], delete=['1.1.1.1:99:na1'])
deleting..
deleting..
deleting..
deleting..
deleting..
import argparse


p = argparse.ArgumentParser()
p.add_argument('-a', '--add', help="input the data in format ip:port:name", nargs='*', default=[])
# add default parameter
p.add_argument('-d', '--delete', help="input the data in format ip:port:name", nargs='*', default=[])
args = p.parse_args()
print(args)
add_List = args.add #['192.168.1.10:80:name1', '172.25.16.2:100:name3']
del_List = args.delete #['192.168.1.10:80:name1', '172.25.16.2:100:name3']

addN = "adding.."
delN = "deleting.."

def test(what):
    for i in range(5):
        print what

######### expected output: "adding.." 5 times
for i in add_List:
    test(addN)

######### expected output: "deleting.." 5 times
for i in del_List:     # <== correct typo
    test(delN)  

答案 1 :(得分:0)

在我看来,这只是一个错字。在第26行del_list而不是del_List。

#!/usr/bin/python

import argparse
import re
import string

p = argparse.ArgumentParser()
p.add_argument('-a', '--add', help="input the data in format ip:port:name", nargs='*')
p.add_argument('-d', '--delete', help="input the data in format ip:port:name", nargs='*')
args = p.parse_args()
add_List = args.add or [] #['192.168.1.10:80:name1', '172.25.16.2:100:name3']
del_List = args.delete or [] #['192.168.1.10:80:name1', '172.25.16.2:100:name3']

addN = "adding.."
delN = "deleting.."

def test(what):
    for i in range(5):
        print(what)

######### expected output: "adding.." 5 times
for i in add_List:
    test(addN)

######### expected output: "deleting.." 5 times
for i in del_List:
    test(delN)

输出

adding..
adding..
adding..
adding..
adding..
deleting..
deleting..
deleting..
deleting..
deleting..

参数数量

--delete 123 --add 456