import argparse
class customAction(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
setattr(args, self.dest, values)
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--example', action=customAction)
I want to pass additional arguments to customAction when the option -e is triggered, e.g. a instance of another class. How can I do this? Everything I have tried has errored out.
Another solution is to derive the based class
argparse.Action
like this:Alternatively, supply
*args
and**kwargs
to pass through any additional parameters to the parent constructor.