Add argparse arguments from external modules
Without trying to fully understand your module structure, I think you want to be able to provide the arguments to a add_argument
call as objects that you can import.
You could, for example, provide a list of positional arguments, and dictionary of keyword arguments:
args=['-f','--foo']
kwargs={'type':int, 'nargs':'*', 'help':'this is a help line'}
parser=argparse.ArgumentParser()
parser.add_argument(*args, **kwargs)
parser.print_help()
producing
usage: ipython [-h] [-f [FOO [FOO ...]]]
optional arguments:
-h, --help show this help message and exit
-f [FOO [FOO ...]], --foo [FOO [FOO ...]]
this is a help line
In argparse.py
, the add_argument
method (of a super class of ArgumentParser
), has this general signature
def add_argument(self, *args, **kwargs):
The code of this method manipulates these arguments, adds the args
to the kwargs
, adds default values, and eventually passes kwargs
to the appropriate Action
class, returning the new action. (It also 'registers' the action with the parser or subparser). It's the __init__
of the Action subclasses that lists the arguments and their default values.
I would just return an ArgumentParser
instance from your get_args
method. Then you can create a new ArgumentParser
to join all other argument parsers using the parents
argument: https://docs.python.org/3/library/argparse.html#parents.