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'
]
Nicholas Knight
16.1k5 gold badges47 silver badges58 bronze badges
asked Feb 17, 2011 at 22:52
Nix
58.9k31 gold badges154 silver badges206 bronze badges
1 Answer 1
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')
answered Feb 17, 2011 at 22:56
Ned Batchelder
378k77 gold badges583 silver badges675 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Dan Gayle
Perfect. I didn't know this module existed.
lang-py