Commits

John Mulligan committed 639b5a7

commander: specific error for mismatched args

Comments (0)

Files changed (2)

test/test_cli_commands.py

             commander.main(GOPT_A, CMDTABLE_A, arguments))
 
     def test_main_dispatch_special(self):
-        sys.stderr = StringIO.StringIO()
         try:
             commander.main(GOPT_A, CMDTABLE_A, 'foo -h'.split())
             caught = False
             caught = True
         self.assertTrue(caught)
 
+    def test_main_dispatch_badargs(self):
+        try:
+            commander.main(GOPT_A, CMDTABLE_A, 'foo x y z'.split())
+            caught = False
+        except:
+            caught = True
+        self.assertTrue(caught)
+        
+
 
             
 class Capture(list):
 
 from vanity import cli
 import sys
+import traceback
 
 
 class InvalidCommand(cli.CliError):
         self.args = args
 
 
+class InvalidArguments(cli.CliError):
+    """The user failed to give correct number of args"""
+    def __init__(self, name):
+        cli.CliError.__init__(self,
+            '%s: unexpected number of arguments' % name)
+
+
 def main(globalopts, cmdtable, arguments, generichelp=None):
     """Launch a subcommand based on the given arguments, automatically
     handling help and cli parsing errors. Will return the
         self.strict = strict
 
     def __call__(self, opts, args):
-        return self.target(*args, **opts)
+        try:
+            return self.target(*args, **opts)
+        except TypeError, err:
+            if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
+                raise InvalidArguments(self.name)
+            raise
 
     def aliaslist(self):
         """Return a list of command aliases.