Customized command line parsing in Python

arguments, command-line, parsing, python, shell

Solution

You could split them up with shlex.split(), which can handle the quoted values you have, and pretty easily parse this with a very simple regular expression. Or, you can just use regular expressions for both splitting and parsing. Or simply use split().

args = {}
for arg in shlex.split(cmdln_args):
    key, value = arg.split('=', 1)
    args[key] = value

Problem

I'm writing a shell for a project of mine, which by design parses commands that looks like this: COMMAND_NAME ARG1="Long Value" ARG2=123 ARG3=me@me.com My problem is that Python's command line parsing libraries (getopt and optparse) forces me to use '-' or '--' in front of the arguments. This behavior doesn't match my requirements. Any ideas how can this be solved? Any existing library for this?

Original source