comparison profilemanager/command.py @ 4:35dc297efa25

adding listing function and other cleanup
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 05 Apr 2010 13:53:22 -0700
parents 4d1cd60dd2a1
children ca57920aa223
comparison
equal deleted inserted replaced
3:4d1cd60dd2a1 4:35dc297efa25
3 """ 3 """
4 4
5 import inspect 5 import inspect
6 import sys 6 import sys
7 from optparse import OptionParser 7 from optparse import OptionParser
8 from pprint import pprint
8 9
9 if 'commands' not in globals(): 10 if 'commands' not in globals():
10 commands = {} 11 commands = {}
11 12
12 def command(function): 13 def command(function):
93 return parser 94 return parser
94 95
95 class CommandParser(OptionParser): 96 class CommandParser(OptionParser):
96 def __init__(self, commands, description=None, setup=None): 97 def __init__(self, commands, description=None, setup=None):
97 usage = '%prog [options] command [command-options]' 98 usage = '%prog [options] command [command-options]'
98 OptionParser.__init__(self, description=description) 99 OptionParser.__init__(self, usage=usage, description=description)
99 for _command in commands: 100 for _command in commands:
100 command(_command) 101 command(_command)
101 self.disable_interspersed_args() 102 self.disable_interspersed_args()
102 self.setup = setup 103 self.setup = setup
103 104
158 commandparser.error("Not enough arguments given") 159 commandparser.error("Not enough arguments given")
159 if len(command_args) != len(command['args']) and not command['varargs']: 160 if len(command_args) != len(command['args']) and not command['varargs']:
160 commandparser.error("Too many arguments given") 161 commandparser.error("Too many arguments given")
161 162
162 # invoke the command 163 # invoke the command
163 print getattr(_object, name)(*command_args, **command_options.__dict__) 164 pprint(getattr(_object, name)(*command_args, **command_options.__dict__))
164 165