chiark / gitweb /
keys/tripe-keys.in: Improve reporting of usage errors.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 9 Jan 2014 08:53:12 +0000 (08:53 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Jan 2014 09:17:03 +0000 (09:17 +0000)
  * 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

index 47a4417c981886a38f2acfe308ac73fd2a50c77d..901e09b96155263c0c8b2783b88a037a6b361a80 100644 (file)
@@ -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 --------------------------------------------------