Commits

Andy Mikhailenko committed af6f75d

Removed option "print_result" from function dispatch(). Fixed bug in type inferring, added regression test.

Comments (0)

Files changed (3)

-version = '0.3.0'
+version = '0.4.0'
         value = kwargs['default']
         if isinstance(value, bool):
             # infer action from default value
-            kwargs['action'] = 'store_true' if value else 'store_false'
+            kwargs['action'] = 'store_false' if value else 'store_true'
         elif 'type' not in kwargs and value is not None:
             # infer type from default value
             kwargs['type'] = type(value)
             command_parser.add_argument(*a_args, **a_kwargs)
         command_parser.set_defaults(function=func)
 
-def dispatch(parser, argv=None, print_result=True, add_help_command=True):
+def dispatch(parser, argv=None, add_help_command=True):
     """Parses given list of arguments using given parser, calls the relevant
     function and prints the result.
 
     :param argv:
         a list of strings representing the arguments. If `None`, ``sys.argv``
         is used instead. Default is `None`.
-    :param print_result:
-        if `True`, the result is printed and returned to the caller. If
-        `False`, it is only returned and not printed. Default is `True`.
     :param add_help_command:
         if `True`, converts first positional argument "help" to a keyword
         argument so that ``help foo`` becomes ``foo --help`` and displays usage
         result = args.function(*ok_args, **ok_kwargs)
     else:
         result = args.function(args)
-    if print_result:
-        print(result)
     return result
 
 
 
 
 @arg('text')
+@arg('--twice', default=False, help='repeat twice')
 def echo(args):
-    return u'you said {0}'.format(args.text)
+    repeat = 2 if args.twice else 1
+    return u'you said {0}'.format(args.text) * repeat
 
 @arg('text')
 @plain_signature
         "A simple command is resolved to a function."
         self.assert_cmd_returns('echo foo', 'you said foo')
 
+    def test_bool_action(self):
+        "Action `store_true`/`store_false` is inferred from default value."
+        self.assert_cmd_returns('echo --twice foo', 'you said fooyou said foo')
+
     def test_plain_signature(self):
         "Arguments can be passed to the function without a Namespace instance."
         self.assert_cmd_returns('plain-echo bar', 'you said bar')