Before I sit down and start hacking it out, I thought I’d come here and see if you all had any tips or even if something like this has been done before.
I want to re-create a basic shell like syntax within a python program. In other words, when people run the file with python, they will be greeted with a little prompt
>>
For simple things, using an option parser to say
opt.parse_args(input.split(" "))
Works just fine, but now I would like to not only escape special characters like spaces with the ‘\’ character, but also treat quoted strings as a single argument, like in a unix shell.
Does there exist anything that might already help with this?
Thanks for any suggestions!
– Chase
Start with the
shlexmodule:You can use it like this:
This just splits things up into logical tokens; it won’t do anything like variable expansion and so forth. That’s still up to you, as is actually running commands.