From: Mark Wooding Date: Mon, 22 Dec 2014 20:32:58 +0000 (+0000) Subject: symm/multigen: Some UI improvements. X-Git-Tag: 2.2.0~7^2~13 X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/commitdiff_plain/f8b70fdb443d01e123cdf5f70ab5df0c5efdfe1a symm/multigen: Some UI improvements. 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. --- diff --git a/symm/multigen b/symm/multigen index f498ce2a..241cd34c 100755 --- a/symm/multigen +++ b/symm/multigen @@ -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 --------------------------------------------------