ArgumentParser parsing optional arguments, not defined in Parser [duplicate]

这一生的挚爱 提交于 2019-12-20 07:32:56

问题


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)

来源:https://stackoverflow.com/questions/54076485/argumentparser-parsing-optional-arguments-not-defined-in-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!