Mercurial > hg > hq
comparison hq/command.py @ 0:b5671297a0db
initial commit of hq
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 30 Apr 2010 14:31:35 -0700 |
parents | |
children | 321721b581f1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b5671297a0db |
---|---|
1 """ | |
2 a command-line interface to the command line, a la pythonpaste | |
3 """ | |
4 | |
5 import inspect | |
6 import sys | |
7 from optparse import OptionParser | |
8 from pprint import pprint | |
9 | |
10 class CommandParser(OptionParser): | |
11 # TODO: add `help` command | |
12 | |
13 def __init__(self, _class, description=None): | |
14 self._class = _class | |
15 self.commands = {} | |
16 usage = '%prog [options] command [command-options]' | |
17 description = description or _class.__doc__ | |
18 OptionParser.__init__(self, usage=usage, description=description) | |
19 commands = [ getattr(_class, i) for i in dir(_class) | |
20 if not i.startswith('_') ] | |
21 commands = [ method for method in commands | |
22 if hasattr(method, '__call__') ] | |
23 for _command in commands: | |
24 self.command(_command) | |
25 self.disable_interspersed_args() | |
26 | |
27 def print_help(self): | |
28 # XXX should probably use the optparse formatters to help out here | |
29 | |
30 OptionParser.print_help(self) | |
31 | |
32 # short descriptions for commands | |
33 command_descriptions = [dict(name=i, | |
34 description=self.commands[i]['doc'].strip().split('\n',1)[0]) | |
35 for i in sorted(self.commands.keys())] | |
36 max_len = max([len(i['name']) for i in command_descriptions]) | |
37 description = "Commands: \n%s" % ('\n'.join([' %s%s %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description']) | |
38 for description in command_descriptions])) | |
39 | |
40 print | |
41 print description | |
42 | |
43 def parse(self, args=sys.argv[1:]): | |
44 """global parse step""" | |
45 | |
46 self.options, args = self.parse_args(args) | |
47 | |
48 # help/sanity check -- should probably be separated | |
49 if not len(args): | |
50 self.print_help() | |
51 sys.exit(0) | |
52 if args[0] == 'help': | |
53 if len(args) == 2: | |
54 if args[1] in self.commands: | |
55 name = args[1] | |
56 commandparser = self.command2parser(name) | |
57 commandparser.print_help() | |
58 else: | |
59 self.error("No command '%s'" % args[1]) | |
60 else: | |
61 self.print_help() | |
62 sys.exit(0) | |
63 command = args[0] | |
64 if command not in self.commands: | |
65 self.error("No command '%s'" % command) | |
66 return command, args[1:] | |
67 | |
68 def invoke(self, args=sys.argv[1:]): | |
69 """ | |
70 invoke | |
71 """ | |
72 | |
73 # parse | |
74 name, args = self.parse(args) | |
75 | |
76 # setup | |
77 _object = self._class(self, self.options) | |
78 | |
79 # command specific args | |
80 command = self.commands[name] | |
81 commandparser = self.command2parser(name) | |
82 command_options, command_args = commandparser.parse_args(args) | |
83 if len(command_args) < len(command['args']): | |
84 commandparser.error("Not enough arguments given") | |
85 if len(command_args) != len(command['args']) and not command['varargs']: | |
86 commandparser.error("Too many arguments given") | |
87 | |
88 # invoke the command | |
89 retval = getattr(_object, name)(*command_args, **command_options.__dict__) | |
90 if isinstance(retval, basestring): | |
91 print retval | |
92 elif retval is None: | |
93 pass | |
94 else: | |
95 pprint(retval) | |
96 return retval | |
97 | |
98 def command(self, function): | |
99 name = function.func_name | |
100 if function.__doc__: | |
101 doc = inspect.cleandoc(function.__doc__) | |
102 else: | |
103 doc = '' | |
104 argspec = inspect.getargspec(function) | |
105 defaults = argspec.defaults | |
106 if defaults: | |
107 args = argspec.args[1:-len(defaults)] | |
108 optional = dict(zip(argspec.args[-len(defaults):], defaults)) | |
109 else: | |
110 args = argspec.args[1:] | |
111 optional = None | |
112 self.commands[name] = { 'doc': doc, | |
113 'args': args, | |
114 'optional': optional, | |
115 'varargs': argspec.varargs | |
116 } | |
117 return function # XXX to restructure??? | |
118 | |
119 def commandargs2str(self, command): | |
120 if isinstance(command, basestring): | |
121 command = self.commands[command] | |
122 retval = [] | |
123 retval.extend(['<%s>' % arg for arg in command['args']]) | |
124 varargs = command['varargs'] | |
125 if varargs: | |
126 retval.append('<%s> [%s] [...]' % (varargs, varargs)) | |
127 if command['optional']: | |
128 retval.append('[options]') | |
129 return ' '.join(retval) | |
130 | |
131 def doc2arghelp(self, docstring, decoration='-', delimeter=':'): | |
132 """ | |
133 Parse a docstring and get at the section describing arguments | |
134 - decoration: decoration character | |
135 - delimeter: delimter character | |
136 | |
137 Yields a tuple of the stripped docstring and the arguments help | |
138 dictionary | |
139 """ | |
140 lines = [ i.strip() for i in docstring.split('\n') ] | |
141 argdict = {} | |
142 doc = [] | |
143 option = None | |
144 for line in lines: | |
145 if not line and option: # blank lines terminate [?] | |
146 break | |
147 if line.startswith(decoration) and delimeter in line: | |
148 name, description = line.split(delimeter, 1) | |
149 name = name.lstrip(decoration).strip() | |
150 description = description.strip() | |
151 argdict[name] = [ description ] | |
152 option = name | |
153 else: | |
154 if option: | |
155 argdict[name].append(line) | |
156 else: | |
157 doc.append(line) | |
158 argdict = dict([(key, ' '.join(value)) | |
159 for key, value in argdict.items()]) | |
160 return ('\n'.join(doc), argdict) | |
161 | |
162 def command2parser(self, command): | |
163 doc, argdict = self.doc2arghelp(self.commands[command]['doc']) | |
164 parser = OptionParser('%%prog %s %s' % (command, self.commandargs2str(command)), | |
165 description=doc, add_help_option=False) | |
166 if self.commands[command]['optional']: | |
167 for key, value in self.commands[command]['optional'].items(): | |
168 help = argdict.get(key, '') | |
169 if value is True: | |
170 parser.add_option('--no-%s' % key, dest=key, | |
171 action='store_false', default=True, | |
172 help=help) | |
173 elif value is False: | |
174 parser.add_option('--%s' % key, action='store_true', | |
175 default=False, help=help) | |
176 else: | |
177 help += ' [DEFAULT: %s]' % value | |
178 parser.add_option('--%s' % key, help=help, default=value) | |
179 | |
180 return parser |