From db76b51b86654f5ef5605f050fc0ad32b61ca619 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Thu, 9 Jan 2014 08:53:12 +0000 Subject: [PATCH] keys/tripe-keys.in: Improve reporting of usage errors. Organization: Straylight/Edgeware From: Mark Wooding * 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. --- keys/tripe-keys.in | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 -------------------------------------------------- -- [mdw]