A beginner here. Need some help on argparse. Below is the code.
#!/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)
What I basically want is that when the script is run as,
- ./script -a 1.1.1.1:99:na1 it should print "adding.." 5 times
- ./script -d 1.1.1.1:99:na1 it should print "deleting.." 5 times
Please do not mind the arguments given with the script cos they will be processed separately. I just want to understand the most efficient way to use argparse with appropriate conditionals in a scenario like this. Further it would be great if I can print out a sensible usage guide as well.
usage? Don't worry about 'efficiency'. This code is run once at the start of your script. Your users are only going to provide a few strings, not hundreds;add_ListanddeL_Listwill be short.