Welcome, guest | Sign In | My Account | Store | Cart
def _method_info_from_argv(argv=None):
    """Command-line -> method call arg processing.
    
    - positional args:
            a b -> method('a', 'b')
    - intifying args:
            a 123 -> method('a', 123)
    - json loading args:
            a '["pi", 3.14, null]' -> method('a', ['pi', 3.14, None])
    - keyword args:
            a foo=bar -> method('a', foo='bar')
    - using more of the above
            1234 'extras=["r2"]'  -> method(1234, extras=["r2"])
    
    @param argv {list} Command line arg list. Defaults to `sys.argv`.
    @returns (<method-name>, <args>, <kwargs>)
    """
    import json
    import sys
    if argv is None:
        argv = sys.argv

    method_name, arg_strs = argv[1], argv[2:]
    args = []
    kwargs = {}
    for s in arg_strs:
        if s.count('=') == 1:
            key, value = s.split('=', 1)
        else:
            key, value = None, s
        try:
            value = json.loads(value) 
        except ValueError:
            pass
        if key:
            kwargs[key] = value
        else:
            args.append(value)
    return method_name, args, kwargs

Diff to Previous Revision

--- revision 1 2010-03-17 13:25:18
+++ revision 2 2010-03-21 22:41:42
@@ -29,12 +29,9 @@
         else:
             key, value = None, s
         try:
-            value = int(value)
+            value = json.loads(value) 
         except ValueError:
-            try:
-                value = json.loads(value) 
-            except ValueError:
-                pass
+            pass
         if key:
             kwargs[key] = value
         else:

History