Comma separated inputs instead of space separated inputs for argparse

前端 未结 5 967
灰色年华
灰色年华 2021-01-19 02:30

I\'m using argparse to receive inputs from the command line to run my script.

My current input string looks like this:

path> python &         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-19 03:11

    You can use module shlex to extract the parameters, then replace commas with spaces, and pass the results to argparse for further processing:

    comma_args = shlex.split("-t T1,T2,T3 -f F1,F2")
    # ['-t', 'T1,T2,T3', '-f', 'F1,F2']
    args = [x.replace(","," ") for x in comma_args]
    # ['-t', 'T1 T2 T3', '-f', 'F1 F2']
    parse_args(args)
    

提交回复
热议问题