Changeset 453 for trunk/moap/extern/command/command.py
- Timestamp:
- 01-11-09 16:44:05 (4 years ago)
- File:
-
- 1 edited
-
trunk/moap/extern/command/command.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/moap/extern/command/command.py
r452 r453 18 18 """ 19 19 _commands = None 20 _aliases = None 20 21 21 22 def addCommand(self, name, description): … … 24 25 self._commands[name] = description 25 26 27 def addAlias(self, alias): 28 if self._aliases is None: 29 self._aliases = [] 30 self._aliases.append(alias) 31 26 32 ### override parent method 27 33 … … 37 43 block)) 38 44 ret = "\n".join(rets) 45 46 # add aliases 47 if self._aliases: 48 ret += "\nAliases: " + ", ".join(self._aliases) + "\n" 49 50 # add subcommands 39 51 if self._commands: 40 52 commandDesc = [] … … 162 174 command.description) 163 175 176 if self.aliases: 177 for alias in self.aliases: 178 formatter.addAlias(alias) 179 164 180 # expand %command for the bottom usage 165 181 usage = self.usage or '' … … 216 232 Override me to implement the functionality of the command. 217 233 """ 218 pass 234 raise NotImplementedError('Implement %s.do()' % self.__class__) 235 # by default, return 1 and hopefully show help 236 return 1 219 237 220 238 def parse(self, argv): … … 279 297 ret = e.status 280 298 self.stderr.write(e.output + '\n') 299 except NotImplementedError: 300 self.parser.print_usage(file=self.stderr) 301 self.stderr.write( 302 "Use --help to get a list of commands.\n") 303 return 1 281 304 282 305 # if everything's fine, we return 0 … … 394 417 395 418 # internal class to subclass cmd.Cmd with a Ctrl-D handler 419 396 420 class CommandCmd(cmd.Cmd): 397 421 prompt = '(command) ' … … 410 434 print 'Exit.' 411 435 412 help_exit = help_EOF 436 def help_exit(self): 437 print 'Exit.' 413 438 414 439 # populate the Cmd interpreter from our command class 415 440 cmdClass = CommandCmd 416 441 417 for name, command in command.subCommands.items(): 442 for name, command in command.subCommands.items() \ 443 + command.aliasedSubCommands.items(): 418 444 if name == 'shell': 419 445 continue … … 422 448 # add do command 423 449 methodName = 'do_' + name 424 def generate(command): 450 451 def generateDo(command): 452 425 453 def do_(s, line): 426 454 # the do_ method is passed a single argument consisting of … … 430 458 command.parse(args) 431 459 return do_ 432 method = generate(command) 460 461 method = generateDo(command) 433 462 setattr(cmdClass, methodName, method) 463 434 464 435 465 # add help command 436 466 methodName = 'help_' + name 437 def generate(command): 467 468 def generateHelp(command): 469 438 470 def help_(s): 439 471 command.parser.print_help(file=command.stdout) 440 472 return help_ 441 method = generate(command) 473 474 method = generateHelp(command) 442 475 setattr(cmdClass, methodName, method) 443 476
Note: See TracChangeset
for help on using the changeset viewer.
