I have the following lines to parse command line arguments:
...
parser = argparse.ArgumentParser(description="Arguments for Creation of delivery report")
parser.add_argument('tag', help="GIT Tag of the Release")
parser.add_argument('--foo')
parser.add_argument('--bar')
parsed_args = parser.parse_args(sys_args)
...
This properly works as intended with a call like:
python my_script.py tag123 --foo foo --bar bar
What I want to achieve is, that users of this script can pass additional "kwargs" as command line arguments, without needing me to define those in the parser via add_argument.
So a call to the script like this:
python my_script.py tag123 --foo foo --bar bar --a 1 --b 2
Should give me:
Namespace(tag='tag123', foo='foo', bar='bar', a='1', b='2')
Is there a way to achieve this?
Fyi: I do not know in advance what additional optional arguments will be given. So extending the parser is not an option. Consider the additional arguments as kind of **kwargs)
python my_script.py tag123 --foo foo --bar bar -- --a 1 --b 2You will haveargs=['1', '--a', 'b']in your NameSpace and can then parse arguments from there.argparsedoes not provide a way of parsing those unrecognized arguments. But you can parse thatextraslist yourself afterwards. A variant on question is handling a bunch of 'key=value' strings. I've seen SO questions like that. But again, the key is you have to parse them yourself.