From: Mark Wooding Date: Thu, 9 Jan 2014 08:53:12 +0000 (+0000) Subject: keys/tripe-keys.in: Improve reporting of usage errors. X-Git-Tag: 1.0.0pre14~3 X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/commitdiff_plain/db76b51b86654f5ef5605f050fc0ad32b61ca619 keys/tripe-keys.in: Improve reporting of usage errors. * Report a readable error for a mis-typed command name, rather than a Python exception backtrace. * Diagnose arguments to a command which expects none correctly. * Remove the `UsageError' exception, which was never caught and is now unused. --- diff --git a/keys/tripe-keys.in b/keys/tripe-keys.in index 47a4417c..901e09b9 100644 --- a/keys/tripe-keys.in +++ b/keys/tripe-keys.in @@ -344,8 +344,9 @@ Subcommands available: args = commands.keys() args.sort() for c in args: - func, min, max, help = commands[c] - print '%s %s' % (c, help) + try: func, min, max, help = commands[c] + except KeyError: die("unknown command `%s'" % c) + print '%s%s%s' % (c, help and ' ', help) ###-------------------------------------------------------------------------- ### Commands: newmaster @@ -554,9 +555,6 @@ def cmd_mtu(args): ###-------------------------------------------------------------------------- ### Main driver. -## Exceptions. -class UsageError (Exception): pass - commands = {'help': (cmd_help, 0, 1, ''), 'newmaster': (cmd_newmaster, 0, 0, ''), 'setup': (cmd_setup, 0, 0, ''), @@ -604,10 +602,12 @@ def main(argv): cmd_help([]) else: c = argv[1] - func, min, max, help = commands[c] + try: func, min, max, help = commands[c] + except KeyError: die("unknown command `%s'" % c) args = argv[2:] - if len(args) < min or (max > 0 and len(args) > max): - raise UsageError, (c, help) + if len(args) < min or (max is not None and len(args) > max): + SYS.stderr.write('Usage: %s %s%s%s\n' % (quis, c, help and ' ', help)) + SYS.exit(1) func(args) ###----- That's all, folks --------------------------------------------------