Python: How can I enable use of kwargs when calling from command line? (perhaps with argparse)
<p>suppose I have the module myscript.py; This module is production code, and is called often as <code>%dir%&gt;python myscript.py foo bar</code>. </p>

<p>I want to extend it to take keyword arguments. I know that I can take these arguments using the script below, but unfortunately one would have to call it using </p>

<p><code>%dir%&gt;python myscript.py main(foo, bar)</code>.</p>

<p>I know that I can use the <code>argparse</code> module, but I'm not sure how to do it. </p>

<pre><code>import sys

def main(foo,bar,**kwargs):
    print 'Called myscript with:'
        print 'foo = %s' % foo
        print 'bar = %s' % bar
        if kwargs:
            for k in kwargs.keys():
                print 'keyword argument : %s' % k + ' = ' + '%s' % kwargs[k]   

if __name__=="__main__":
    exec(''.join(sys.argv[1:]))
</code></pre>