merlin2011
merlin2011

Reputation: 75555

Is there a python standard function to parse a string into an argv list exactly like bash does, in Python?

Consider the following string:

./kmux.py  -r 'messenger.* xx'

If kmux.py is a python script, and the string above was given to bash as an argument, then sys.argv will be the following list:

["./kmux.py", "-r", "messenger.* xx"]

Is there a standard python function that will take the first string and turn it into the array?

I could manually replicate bash's logic in Python to take the original string and turn it into this array, but I am curious about whether such a function already exists.

Upvotes: 2

Views: 122

Answers (1)

hazrmard
hazrmard

Reputation: 3661

Use the default shlex package like so:

import shlex
shlex.split("'./kmux.py  -r 'messenger.* xx'")

Upvotes: 2

Related Questions