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'
]

1 Answer 1

11
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. I didn't know this module existed.

Your Answer

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.