Andy Mikhailenko committed 91c3613

Re #6: added a basic test and the relevant code for a default command for the root parser. It seems that everything is fine but we need to check for conflicts, etc.

Comments (0)

Files changed (2)

 if PY3:
     def raw_input(text):
         return input(text.decode())
 __all__ = [
-    'ArghParser', 'add_commands', 'autocomplete', 'dispatch', 'confirm',
-    'wrap_errors'
+    'ArghParser', 'add_commands', 'autocomplete', 'confirm', 'dispatch',
+    'set_default_command', 'wrap_errors'
+def set_default_command(parser, function):
+    for a_args, a_kwargs in getattr(function, ATTR_ARGS, []):
+        parser.add_argument(*a_args, **a_kwargs)
+    parser.set_defaults(function=function)
 def add_commands(parser, functions, namespace=None, title=None,
                  description=None, help=None):
     """Adds given functions as commands to given parser.
         cmd_name = getattr(func, ATTR_ALIAS, func.__name__.replace('_','-'))
         cmd_help = func.__doc__
         command_parser = subparsers.add_parser(cmd_name, help=cmd_help)
-        for a_args, a_kwargs in getattr(func, ATTR_ARGS, []):
-            command_parser.add_argument(*a_args, **a_kwargs)
-        command_parser.set_defaults(function=func)
+        set_default_command(command_parser, func)
 def dispatch(parser, argv=None, add_help_command=True, encoding=None,
              completion=True, pre_call=None, output_file=sys.stdout,
 def _encode(line, output_file, encoding=None):
     """Converts given string to given encoding. If no encoding is specified, it
     is determined from terminal settings or, if none, from system settings.
     # Convert string from Unicode to the output encoding
     return line.encode(encoding)
 def _execute_command(args):
     """Asserts that ``args.function`` is present and callable. Tries different
     approaches to calling the function (with an `argparse.Namespace` object or
     wrappers for stand-alone functions :func:`add_commands` ,
     :func:`autocomplete` and :func:`dispatch`.
+    def set_default_command(self, *args, **kwargs):
+        "Wrapper for :func:`set_command`."
+        return set_default_command(self, *args, **kwargs)
     def add_commands(self, *args, **kwargs):
         "Wrapper for :func:`add_commands`."
         return add_commands(self, *args, **kwargs)
         return default
     return None
 def wrap_errors(*exceptions):
     """Decorator. Wraps given exceptions into :class:`CommandError`. Usage::
         self.assert_cmd_returns('', b(self.parser.format_usage()+'\n'))
+class DefaultCommandTestCase(BaseArghTestCase):
+    def setUp(self):
+        self.parser = DebugArghParser('PROG')
+        @arg('--foo', default=1)
+        def main(args):
+            return
+        self.parser.set_default_command(main)
+    def test_default_command(self):
+        self.assert_cmd_returns('', b('1\n'))
+        self.assert_cmd_returns('--foo 2', b('2\n'))
+        self.assert_cmd_exits('--help')
 class ConfirmTestCase(unittest.TestCase):
     def assert_choice(self, choice, expected, **kwargs):
         argh.helpers.raw_input = lambda prompt: choice