Is there a way to implement **kwargs behavior when calling a Python script from the command line
Question: Say I have a function as follows:

    
    
    def foo(**kwargs):
        print kwargs
    

And then call the function like this, I get this handy little dict of all
`kwargs`.

    
    
    >>> foo(a = 5, b = 7)
    {'a': 5, 'b': 7}
    

I want to do this directly to scripts I call from command line. So entering
this:

    
    
    python script.py a = 5 b = 7
    

Would create a similar dict to the example above. Can this be done?

**Here's what I have so far:**

    
    
    import sys
    
    kwargs_raw = sys.argv[1:]
    kwargs = {key:val for key, val in zip(kwargs_raw[::3], kwargs_raw[1::3])}
    print kwargs
    

And here's what this produces:

    
    
    Y:\...\Python>python test.py a = 5 b = 7
    {'a': '5', 'b': '7'}
    

So you may be wondering why this isn't good enough

  1. Its very structured, and thus, won't work if `a` or `b` are anything other that strings, ints, or floats.
  2. I have no way of determining if the user intended to have 5 be an int, string, or float

I've seen `ast.literal_eval()` around here before, but I couldn't figure out
how to get that to work. Both my attempts failed:

    
    
    >>> ast.literal_eval("a = 5")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "Y:\admin\Anaconda\lib\ast.py", line 49, in literal_eval
        node_or_string = parse(node_or_string, mode='eval')
      File "Y:\admin\Anaconda\lib\ast.py", line 37, in parse
        return compile(source, filename, mode, PyCF_ONLY_AST)
      File "<unknown>", line 1
        a = 5
    

and

    
    
    >>> ast.literal_eval("{a:5,b:7}")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "Y:\admin\Anaconda\lib\ast.py", line 80, in literal_eval
        return _convert(node_or_string)
      File "Y:\admin\Anaconda\lib\ast.py", line 63, in _convert
        in zip(node.keys, node.values))
      File "Y:\admin\Anaconda\lib\ast.py", line 62, in <genexpr>
        return dict((_convert(k), _convert(v)) for k, v
      File "Y:\admin\Anaconda\lib\ast.py", line 79, in _convert
        raise ValueError('malformed string')
    ValueError: malformed string
    

If it matters, I'm using Python 2.7.6 32-bit on Windows 7 64-bit. Thanks in
advance


Answer: It seems what you're _really_ looking for is a way to parse command-line
arguments. Take a look at the `argparse` module:
<http://docs.python.org/2/library/argparse.html#module-argparse>

Alternately, if you really want to give your arguments in dictionary-ish form,
just use the `json` module:

    
    
    import json, sys
    
    # Run your program as:
    # python my_prog.py "{'foo': 1, 'bar': 2}"
    # (the quotes are important)
    data = json.loads(sys.argv[1])
    

