comparison commandparser/command.py @ 8:109627b7db9f

add ability to have mandatory class arguments
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 30 Mar 2012 10:37:39 -0700
parents 005e073dc590
children 0069096e8e22
comparison
equal deleted inserted replaced
7:0abe38b2ea7b 8:109627b7db9f
19 # TODO: add `help` command 19 # TODO: add `help` command
20 20
21 def __init__(self, _class, description=None): 21 def __init__(self, _class, description=None):
22 self._class = _class 22 self._class = _class
23 self.commands = {} 23 self.commands = {}
24 usage = '%prog [options] command [command-options]' 24 init = self.command(_class.__init__)
25 self.init_args = init['args']
26 command_str = ' '.join(self.init_args + ['command'])
27 usage = '%prog [options]' + ' %s [command-options]' % (command_str)
25 description = description or _class.__doc__ 28 description = description or _class.__doc__
26 OptionParser.__init__(self, usage=usage, description=description) 29 OptionParser.__init__(self, usage=usage, description=description)
27 commands = [ getattr(_class, i) for i in dir(_class) 30 commands = [ getattr(_class, i) for i in dir(_class)
28 if not i.startswith('_') ] 31 if not i.startswith('_') ]
29 commands = [ method for method in commands 32 commands = [ method for method in commands
31 for _command in commands: 34 for _command in commands:
32 c = self.command(_command) 35 c = self.command(_command)
33 self.commands[c['name']] = c 36 self.commands[c['name']] = c
34 37
35 # get class options 38 # get class options
36 init = self.command(_class.__init__)
37 self.command2parser(init, self) 39 self.command2parser(init, self)
38
39 self.disable_interspersed_args() 40 self.disable_interspersed_args()
40 41
41 def add_option(self, *args, **kwargs): 42 def add_option(self, *args, **kwargs):
42 kwargs['default'] = Undefined(kwargs.get('default')) 43 kwargs['default'] = Undefined(kwargs.get('default'))
43 OptionParser.add_option(self, *args, **kwargs) 44 OptionParser.add_option(self, *args, **kwargs)
79 else: 80 else:
80 self.error("No command '%s'" % args[1]) 81 self.error("No command '%s'" % args[1])
81 else: 82 else:
82 self.print_help() 83 self.print_help()
83 sys.exit(0) 84 sys.exit(0)
85 required = len(self.init_args) + 1 # command
86 if len(args) < required:
87 self.print_usage()
88 sys.exit(1)
89 self.command_args = args[:len(self.init_args)]
90 args = args[len(self.init_args):]
84 command = args[0] 91 command = args[0]
85 if command not in self.commands: 92 if command not in self.commands:
86 self.error("No command '%s'" % command) 93 self.error("No command '%s'" % command)
87 return command, args[1:] 94 return command, args[1:]
88 95
90 """ 97 """
91 invoke 98 invoke
92 """ 99 """
93 100
94 # parse 101 # parse
95 name, args = self.parse(args) 102 name, args = self.parse(args)
96 103
97 # setup 104 # setup
98 options = {} 105 options = {}
99 dotfile = os.path.join(os.environ['HOME'], '.' + self.get_prog_name()) 106 dotfile = os.path.join(os.environ['HOME'], '.' + self.get_prog_name())
100 if os.path.exists(dotfile): 107 if os.path.exists(dotfile):
114 if key in options: 121 if key in options:
115 continue 122 continue
116 options[key] = value.default 123 options[key] = value.default
117 else: 124 else:
118 options[key] = value 125 options[key] = value
119 _object = self._class(**options) 126 _object = self._class(*self.command_args, **options)
120 127
121 # command specific args 128 # command specific args
122 command = self.commands[name] 129 command = self.commands[name]
123 commandparser = self.command2parser(name) 130 commandparser = self.command2parser(name)
124 command_options, command_args = commandparser.parse_args(args) 131 command_options, command_args = commandparser.parse_args(args)