Can argparse accept argument value as key=val pairs

后端 未结 2 1082
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 12:06

I am trying to implement below option by using argparse(can\'t use any other tool like docopt because of project requirement):-

cli.py --conf key1=value1, k         


        
2条回答
  •  执念已碎
    2020-12-21 12:52

    I would use type=str and parse it later, perhaps with json, but the main problem with that would be the way you are writing your command:

    cli.py --conf key1=value1, key2=value2, kay3=value3
    

    The spaces split each pair to a different argument. If you use either method you suggested it would not work. Instead make sure that they are all one argument:

    cli.py --conf="key1='value1', key2='value2', kay3='value3'"
    

    That way this large argument is ready to be parsed as json

提交回复
热议问题