20
20
return add_dict('_helps', **kwargs)
24
23
def get_function_parser(function):
24
"""Generate an OptionParser for a function.
26
Defaults come from the parameter defaults.
27
For every permitted to provide as an option, the type must be specified,
28
using the types decorator.
29
Help may be specified using the helps decorator.
25
31
parser = OptionParser()
26
32
args, ignore, ignored, defaults = inspect.getargspec(function)
45
"""Base class for subcommands."""
50
def parse_args(cls, command, args):
52
raise UserError('Too many arguments.')
56
def run_from_args(cls, command, cmd_args):
57
parser = get_function_parser(command)
58
options, args = parser.parse_args(cmd_args)
59
kwargs = cls.parse_args(command, args)
60
kwargs.update(options.__dict__)
64
def run_subcommand(cls, argv):
66
raise UserError('Must supply a command: %s.' %
67
', '.join(cls.commands.keys()))
69
command = cls.commands[argv[0]]
71
raise UserError('%s invalid. Valid commands: %s.' %
72
(argv[0], ', '.join(cls.commands.keys())))
73
cls.run_from_args(command, argv[1:])
50
def parse_args(command, args):
51
"""Return the positional arguments as a dict."""
54
raise UserError('Too many arguments.')
58
def run_from_args(command, cmd_args):
59
"""Run a command function using the specified commandline arguments."""
60
parser = get_function_parser(command)
61
options, args = parser.parse_args(cmd_args)
62
kwargs = parse_args(command, args)
63
kwargs.update(options.__dict__)
67
def run_subcommand(subcommands, argv):
68
"""Run a subcommand as specified by argv."""
70
raise UserError('Must supply a command: %s.' %
71
', '.join(subcommands.keys()))
73
command = subcommands[argv[0]]
75
raise UserError('%s invalid. Valid commands: %s.' %
76
(argv[0], ', '.join(subcommands.keys())))
77
run_from_args(command, argv[1:])