简体   繁体   中英

Python String Argument Parsing

I am working with the cmd class in python and it passes me all of my arguments as one big string. What is the best way to tokenize this arg string into an array of args[].

Example:

args = 'arg arg1 "arg2 with quotes" arg4 arg5=1'
result = split_args(args)

And it would look like:

 result = [  
      'arg',
      'arg1',
      'arg2 with quotes',
      'arg4',
      'arg5=1'
]
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM