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.
op = OP.OptionParser(
description = 'Generates files by filling in simple templates',
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@')
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(
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.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')
opts, args = op.parse_args()
if len(args) < 1: op.error('missing FILE')
templ.subst(out, cs)
OS.rename(new, file)
else:
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 --------------------------------------------------
###----- That's all, folks --------------------------------------------------