From db32ee67f94b55d2bcb52df31408377f012e820f Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sun, 11 Apr 2010 19:26:32 +0200 Subject: [PATCH] check commands arguments without catching TypeError exception --- weboob/tools/application/console.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index f9108e89..78be4b8d 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -79,14 +79,19 @@ class ConsoleApplication(BaseApplication): if len(matching_commands) == 0: sys.stderr.write("No such command: %s.\n" % command) elif len(matching_commands) == 1: - try: - return getattr(self, matching_commands[0])(*args) - except TypeError, e: - try: - sys.stderr.write("Command '%s' takes %s arguments.\n" % \ - (command, int(str(e).split(' ')[3]) - 1)) - except: - sys.stderr.write('%s\n' % e) + func = getattr(self, matching_commands[0]) + + _args, varargs, varkw, defaults = getargspec(func) + nb_args = len(_args) - 1 + if defaults: + nb_args -= len(defaults) + if len(args) < nb_args or len(args) > nb_args and not varargs: + if varargs: + sys.stderr.write("Command '%s' takes at least %d arguments.\n" % (command, nb_args)) + else: + sys.stderr.write("Command '%s' takes %d arguments.\n" % (command, nb_args)) + return + return func(*args) else: sys.stderr.write("Ambiguious command %s: %s.\n" % (command,