I am trying to configure argparse to allow me to specify arguments that will be passed onto another module down the road. My desired functionality would allow me to insert arguments such as -A "-f filepath" -A "-t"
and produce a list such as ['-f filepath', '-t']
.
In the docs it seems that adding action='append'
should do exactly this - however I am getting an error when attempting to specify the -A
argument more than once.
Here is my argument entry:
parser.add_argument('-A', '--module-args',
help="Arg to be passed through to the specified module",
action='append')
Running python my_program.py -A "-k filepath" -A "-t"
produces this error from argparse:
my_program.py: error: argument -A/--module-args: expected one argument
Minimal example:
from mdconf import ArgumentParser
import sys
def parse_args():
parser = ArgumentParser()
parser.add_argument('-A', '--module-args',
help="Arg to be passed through to the module",
action='append')
return parser.parse_args()
def main(args=None):
try:
args = parse_args()
except Exception as ex:
print("Exception: {}".format(ex))
return 1
print(args)
return 0
if __name__ == "__main__":
sys.exit(main())
Any ideas? I find it strange that it is telling me that it expects one argument when the append
should be putting these things into a list.