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