Popular recipes tagged "argv"http://code.activestate.com/recipes/tags/argv/2010-03-21T22:41:42-07:00ActiveState Code Recipestransform command line arguments to args and kwargs for a method call (Python) 2010-03-21T22:41:42-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577122-transform-command-line-arguments-to-args-and-kwarg/ <p style="color: grey"> Python recipe 577122 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/argv/">argv</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/json/">json</a>). Revision 2. </p> <p>For many of my Python modules I find it convenient to provide a small <code>if __name__ == "__main__": ...</code> block to be able to call individual methods from the command line. This requires some kind of translation of command-line string arguments to <code>args</code> and <code>kwargs</code> for the method call. This recipe uses a few conventions to do that:</p> <ul> <li>the first argument is the method name</li> <li>positional args are positional (duh)</li> <li>"key=value" is a keyword argument</li> <li>an attempt is made to interpret arguments as JSON to allow specifying types other than string</li> </ul>