How do you convert command line args in python to a dictionary?

priestc picture priestc · Oct 9, 2012 · Viewed 18.6k times · Source

I'm writing an application that takes arbitrary command line arguments, and then passes them onto a python function:

$ myscript.py --arg1=1 --arg2=foobar --arg1=4

and then inside myscript.py:

import sys
argsdict = some_function(sys.argv)

where argsdict looks like this:

{'arg1': ['1', '4'], 'arg2': 'foobar'}

I'm sure there is a library somewhere that does this, but I can't find anything.

EDIT: argparse/getopt/optparse is not what I'm looking for. These libraries are for defining an interface that is the same for each invocation. I need to be able to handle arbitrary arguments.

Unless, argparse/optparse/getopt has functionality that does this...

Answer

chepner picture chepner · Oct 13, 2012

Here's an example using argparse, although it's a stretch. I wouldn't call this complete solution, but rather a good start.

class StoreInDict(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        d = getattr(namespace, self.dest)
        for opt in values:
            k,v = opt.split("=", 1)
            k = k.lstrip("-")
            if k in d:
                d[k].append(v)
            else:
                d[k] = [v]
        setattr(namespace, self.dest, d)

# Prevent argparse from trying to distinguish between positional arguments
# and optional arguments. Yes, it's a hack.
p = argparse.ArgumentParser( prefix_chars=' ' )

# Put all arguments in a single list, and process them with the custom action above,
# which convertes each "--key=value" argument to a "(key,value)" tuple and then
# merges it into the given dictionary.
p.add_argument("options", nargs="*", action=StoreInDict, default=dict())

args = p.parse_args("--arg1=1 --arg2=foo --arg1=4".split())
print args.options