Sherry Sherry - 1 year ago 142
Python Question

Calling a function using argparse

I am trying to call the function addPXT through argparse when type -a. Its not doing that. Read another issue, there addPXT was not in colons, tried that it says addPXT is not callable.

parser = argparse.ArgumentParser()
parser.add_argument('-a' ,action='store_const' ,const='addPXT')
results = parser.parse_args()

def addPXT():
print "hello"

python -a

Answer Source

If you are a beginner with argparse and python, I'd recommend sticking with the default store action, which stores strings, and the boolean actions ('store_true/false'). Make sure you understand those first.

That said, here is a way of using store_const to call different functions:

In [131]: import argparse

define 2 functions:

In [132]: def act1():
     ...:     print('act1')
In [133]: def act2():
     ...:     print('act2')
In [134]: parser=argparse.ArgumentParser()
In [135]: parser.add_argument('-a',action='store_const',default=act1,const=act2);

I define both the default and the const - and specify the functions, not their names. Understanding the difference is important.

Try the default case:

In [136]: args=parser.parse_args([])
In [137]: print(args)
Namespace(a=<function act1 at 0xb07331dc>)
In [138]: args.a()

Try the -a commandline case:

In [139]: args=parser.parse_args(['-a'])
In [140]: print(args)
Namespace(a=<function act2 at 0xb078c1dc>)
In [141]: args.a()

If you have more arguments (dest), you could pass args to your function, if it is defined to accept them, args.a(args).

The simpler boolean argument approach:

In [146]: parser=argparse.ArgumentParser()
In [147]: parser.add_argument('-a',action='store_true');
In [148]: args=parser.parse_args([])
In [149]: print(args)
In [150]: if args.a:
     ...:     act2()
     ...: else:
     ...:     act1()
# similarly for `['-a']`.

or if you accept strings, maybe even choices

if args.a == 'act1':
elif ...

The primary purpose of argparse is to deduce what the user wants, and issue help and error messages. Acting on that information is largely the responsibility of the rest your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download