0

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,

  1. ./script -a 1.1.1.1:99:na1 it should print "adding.." 5 times
  2. ./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.

10
  • 1
    take a look at click Commented Apr 29, 2017 at 1:09
  • @Azat Ibrakov click looks promising. Thanks a lot. Id still love to get it done with argparse so please ammend the code if you know your way around argparse. thanks. Commented Apr 29, 2017 at 1:14
  • 1
    What's the problem? Does it work? Produce what you want, if not what is wrong? What do you mean by sensible 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_List and deL_List will be short. Commented Apr 29, 2017 at 1:16
  • I've mentioned what I want. When executed with -d it should print "deleting" and with -a "adding". Commented Apr 29, 2017 at 1:20
  • But what does it do NOW? Commented Apr 29, 2017 at 1:21

2 Answers 2

1

When I first run your code as posted I get

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

If I correct the typo (upper case 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

Here the problem is that the default is None. for i in None: does not work. So we either need to test for None, e.g.

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

or we need change the default to something like default=[]. Also I"ve added a print(args) statement, so we get a clearer idea of what the parser produces.

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)  
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. Please if you don't mind can yo post the full code for clarity? Having some troubles understanding.
0

It seem to me that it is just a typo. On row 26 del_list instead of 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)

Output

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

Args

--delete 123 --add 456

2 Comments

What if he doesn't give --delete at all?
@Diego Amicabile Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.