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