0

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 script.py -a
7
  • 1
    Why did you think it would do that? Commented Nov 3, 2016 at 18:10
  • I am trying to call a function, read another issue. this was recommended Commented Nov 3, 2016 at 18:12
  • Could you link to that, and provide an actual minimal reproducible example? By colons do you mean quotes? Commented Nov 3, 2016 at 18:13
  • The const argument of add_argument() is used to hold constant values -- Are you sure that should be the function name? Commented Nov 3, 2016 at 18:14
  • What is your goal? argparse is for parsing command line arguments and not for calling arbitrary functions. If addPTX is supposed to process an argument, for later use by your program, then great. But read how the action keyword works. It has a specific role and it uses an Action object. Commented Nov 3, 2016 at 18:22

3 Answers 3

7

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()
act1

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()
act2

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)
Namespace(a=False)
In [150]: if args.a:
     ...:     act2()
     ...: else:
     ...:     act1()
act1
# similarly for `['-a']`.

or if you accept strings, maybe even choices

if args.a == 'act1':
     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.

Sign up to request clarification or add additional context in comments.

8 Comments

stackoverflow.com/questions/19251204/execute-function-via-arg I tried with type also didn't work for me.
Make sure you understand when the type function is executed. See my comment on the accepted answer of your link.
parser.add_argument('-a' ,action='store_const',default='addPXT',const='test'); I tried to use ur method, I am not able to give default and const as function names, its accepting as a string only. when I tries results.a() it says str not callable. On the other hand if i remove colons i get addPXT not defined
its accepting as a string only - elaborate. What's the error message? You need to understand the difference between function names (references) and strings. Strings aren't callable. The end of my code shows how string values could be used.
Traceback (most recent call last): File "PXT.py", line 17, in <module> parser.add_argument('-a' ,action='store_const',default='addPXT',const=test); NameError: name 'test' is not defined
|
3

argparse will call the action object during parsing but you need to supply something that looks like the Action class because the parser will use that object later. The Action doc says

You may also specify an arbitrary action by passing an Action subclass or other object that implements the same interface. The recommended way to do this is to extend Action, overriding the call method and optionally the init method.

So, create an Action subclass and have it call your function

import argparse
import sys

def addPXT():
    print "hello"

class FooAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        addPXT()

parser = argparse.ArgumentParser()
parser.add_argument('-a', action=FooAction)
results = parser.parse_args(sys.argv[1:])

Comments

0

You're missing a destoption in the add_argument to be able to reference the option.

You will then need to test if the -a option was passed as argument. If so, then call the function.

parser = argparse.ArgumentParser()
parser.add_argument('-a' ,dest="my_flag", action='store_const'  ,const=True)
results = parser.parse_args()

def addPXT():
    print "hello"

if results.my_flag:
    addPXT()

5 Comments

Whether this is the right answer depends on OP's intent. If OP wants to define an alternate action for the command parser, an Action object would be more appropriate.
Right, but as far as I know the action keyword does not support function calling in py3 docs.python.org/3/library/argparse.html#action
Use `action='store_true' to get a simple True/False flag. For a beginner Python/argparse user simple True/False and string arguments are best.
Without the dest, results.a fetches the value. Or use ('-a','--my_flag',...).
The action keyword expects a Action-like object which should do some preprocessing on the option before the parser continues on its way. Depending on what OP wants to do, either your answer or something using an Action object is best.

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.