'Python argparse - user defined Action with 'store_true' behavoiur

Hi I suppose that i have parser argument which cannot pass any value, for example:

parser.add_argument('-s', '--staged', action=FooAction)

And my user defined action:

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

When i trying call this without any values:

python my_script -s

I receive this error message:

test.py: error: argument -s/--staged: expected one argument

I know that i can add action 'store_true' to my argument, but in this solution i cannot redirect execution of this argument to my defined action class.

Is someone know how to modify FooAction to achieve 'store_true" action behaviour?

edit

class Fooaction with set nargs=0:

class FooAction(argparse.Action):
    def __init__(self, option_strings, dest, nargs=0, **kwargs):
        super(FooAction, self).__init__(option_strings, dest, nargs, **kwargs)
    def __call__(self, parser, namespace, values, option_string=None):
        print("action")


Solution 1:[1]

import argparse

class FooAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        print("action")
        print(self)
        print(parser, namespace, values, option_string)
        
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--staged', action=FooAction, nargs=0)
args = parser.parse_args()
print(args)

sample run:

1151:~/mypy$ python3 stack56348020.py -s
action
FooAction(option_strings=['-s', '--staged'], dest='staged', nargs=0, const=None, default=None, type=None, choices=None, help=None, metavar=None)
ArgumentParser(prog='stack56348020.py', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True) Namespace(staged=None) [] -s
Namespace(staged=None)

Using your init

import argparse

class FooAction(argparse.Action):
    def __init__(self, option_strings, dest, nargs=0, **kwargs):
        super(FooAction, self).__init__(option_strings, dest, nargs=nargs, **kwargs)
    def __call__(self, parser, namespace, values, option_string=None):
        print("action")
        print(parser, namespace, values, option_string)
        
parser = argparse.ArgumentParser()
a1 = parser.add_argument('-s', '--staged', action=FooAction)
print(a1)   # display the action object and most of its parameters
args = parser.parse_args()
print(args)

1208:~/mypy$ python3 stack56348020.py -s
FooAction(option_strings=['-s', '--staged'], dest='staged', nargs=0, const=None, default=None, type=None, choices=None, help=None, metavar=None)
action
ArgumentParser(prog='stack56348020.py', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True) Namespace(staged=None) [] -s
Namespace(staged=None)

add_argument returns the Action object it created. We usually ignore it, but saving it to a reference, and printing it can be handy during debugging.

Even though the Action subclasses are marked as 'private' (with _) it's a good idea to look at their code to see how they customize the base class. Action class by itself doesn't do much. The default action uses _StoreAction. Store True/False subclass _StoreConst. _HelpAction is the only one that does not store some sort of value in the namespace; and it does a system exit.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1