Using argparse to parse arguments of form “arg= val”

后端 未结 4 1434
时光说笑
时光说笑 2020-12-14 18:20

I want to use argparse to parse command lines of form \"arg=val\" For example, the usage would be:

script.py conf_dir=/tmp/good_conf

To ach

4条回答
  •  死守一世寂寞
    2020-12-14 18:56

    You need a custom action

    class StoreNameValuePair(argparse.Action):
        def __call__(self, parser, namespace, values, option_string=None):
            n, v = values.split('=')
            setattr(namespace, n, v)
    
    args = parser.add_argument("conf_dir", action=StoreNameValuePair)
    

提交回复
热议问题