symm/multigen: Some UI improvements.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 22 Dec 2014 20:32:58 +0000 (20:32 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 15 Mar 2015 02:20:08 +0000 (02:20 +0000)
Previously the usage message (and the code!) suggested that you could
run multigen without any options, and it'd default to generating
output.  Well, that was true, only it wouldn't know which input file to
read and then it'd crash with the unusually unhelpful error

  TypeError: coercing to Unicode: need string or buffer, NoneType found

Changes to fix this:

  * There's no default mode any more.  You have to give `-l' or `-g'.
    The `-g' option now does double duty, setting the mode and storing
    an input file name.

  * Print the `no mode set' error in a more friendly way, now that we
    actually expect to see it.

  * Fix the usage message so that it's clear that `-g' takes an
    argument, which it always used to anyway.

  * Change the usage message so that it shows `-l' and `-g' as
    alternatives from which you must select one, rather than just
    optional things from which you might pick several.

Callers which worked before won't see any change.  Interactive use is
now a bit less dreadful.

symm/multigen

index f498ce2..241cd34 100755 (executable)
@@ -741,17 +741,21 @@ def compile_template(file, text):
 
 op = OP.OptionParser(
   description = 'Generates files by filling in simple templates',
-  usage = 'usage: %prog [-gl] FILE [COL,...=VAL,... ... | @FILE:COL,...] ...',
+  usage = 'usage: %prog {-l | -g TMPL} FILE [COL,...=VAL,... ... | @FILE:COL,...] ...',
   version = 'Catacomb version @VERSION@')
+def cb_gen(opt, optstr, arg, op):
+  op.values.input = arg
+  op.values.mode = 'gen'
 for short, long, kw in [
   ('-l', '--list', dict(
       action = 'store_const', const = 'list', dest = 'mode',
       help = 'list filenames generated')),
   ('-g', '--generate', dict(
-      action = 'store', metavar = 'PATH', dest = 'input',
-      help = 'generate output (default)'))]:
+      action = 'callback', metavar = 'TEMPLATE',
+      callback = cb_gen, type = 'string',
+      help = 'generate file(s) from TEMPLATE file'))]:
   op.add_option(short, long, **kw)
-op.set_defaults(mode = 'gen')
+op.set_defaults(mode = 'what?')
 opts, args = op.parse_args()
 
 if len(args) < 1: op.error('missing FILE')
@@ -787,6 +791,6 @@ elif opts.mode == 'gen':
       templ.subst(out, cs)
     OS.rename(new, file)
 else:
-  raise Exception, 'What am I doing here?'
+  die('What am I doing here?')
 
 ###----- That's all, folks --------------------------------------------------