# HG changeset patch # User John Mulligan # Date 1257119230 18000 # Sun Nov 01 18:47:10 2009 -0500 # Node ID 639b5a7f1ff64826da4b0f6be1ddd1ceeead2d53 # Parent 8f477e723d7ca5b73948efd20c3e8dfb04b5eb4f commander: specific error for mismatched args diff --git a/test/test_cli_commands.py b/test/test_cli_commands.py --- a/test/test_cli_commands.py +++ b/test/test_cli_commands.py @@ -216,7 +216,6 @@ 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 @@ -236,6 +235,15 @@ 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): diff --git a/vanity/commander.py b/vanity/commander.py --- a/vanity/commander.py +++ b/vanity/commander.py @@ -19,6 +19,7 @@ from vanity import cli import sys +import traceback class InvalidCommand(cli.CliError): @@ -49,6 +50,13 @@ 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 @@ -207,7 +215,12 @@ 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.