3

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

1 Answer 1

11
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')
answered Feb 17, 2011 at 22:56
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. I didn't know this module existed.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.