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
  • 15,774
  • 5
  • 45
  • 57
Nix
  • 57,072
  • 29
  • 149
  • 198

1 Answers1

11
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662