Parsing an unknown number of positional arguments is possible by setting nargs
to *
.
Is there any way to parse an unknown number of named arguments with argparse?
For example:
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("optionals", nargs="**")
>>> parser.parse_args(['--foo', 'FOO', '--bar', 'BAR'])
Namespace(optionals={'foo': 'FOO', 'bar': 'BAR'})
I can try and parse the resulting list that comes out of nargs='*'
but that will prove to be inefficient with lots of edge-cases.