Python String Argument Parsing

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

Example:

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

And it will look like this:

 result = [  
      'arg',
      'arg1',
      'arg2 with quotes',
      'arg4',
      'arg5=1'
]
+3
source share
1 answer
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')
+11
source

Source: https://habr.com/ru/post/1792929/


All Articles