Ignore:
Timestamp:
01-11-09 16:44:05 (4 years ago)
Author:
thomas
Message:
  • command.py: Add aliases to the interpreter as well.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/moap/extern/command/command.py

    r452 r453  
    1818    """ 
    1919    _commands = None 
     20    _aliases = None 
    2021 
    2122    def addCommand(self, name, description): 
     
    2425        self._commands[name] = description 
    2526 
     27    def addAlias(self, alias): 
     28        if self._aliases is None: 
     29            self._aliases = [] 
     30        self._aliases.append(alias) 
     31 
    2632    ### override parent method 
    2733 
     
    3743                block)) 
    3844        ret = "\n".join(rets) 
     45 
     46        # add aliases 
     47        if self._aliases: 
     48            ret += "\nAliases: " + ", ".join(self._aliases) + "\n" 
     49 
     50        # add subcommands 
    3951        if self._commands: 
    4052            commandDesc = [] 
     
    162174                    command.description) 
    163175 
     176        if self.aliases: 
     177            for alias in self.aliases: 
     178                formatter.addAlias(alias) 
     179 
    164180        # expand %command for the bottom usage 
    165181        usage = self.usage or '' 
     
    216232        Override me to implement the functionality of the command. 
    217233        """ 
    218         pass 
     234        raise NotImplementedError('Implement %s.do()' % self.__class__) 
     235        # by default, return 1 and hopefully show help 
     236        return 1 
    219237 
    220238    def parse(self, argv): 
     
    279297                ret = e.status 
    280298                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 
    281304 
    282305            # if everything's fine, we return 0 
     
    394417 
    395418    # internal class to subclass cmd.Cmd with a Ctrl-D handler 
     419 
    396420    class CommandCmd(cmd.Cmd): 
    397421        prompt = '(command) ' 
     
    410434            print 'Exit.' 
    411435 
    412         help_exit = help_EOF 
     436        def help_exit(self): 
     437            print 'Exit.' 
    413438 
    414439    # populate the Cmd interpreter from our command class 
    415440    cmdClass = CommandCmd 
    416441 
    417     for name, command in command.subCommands.items(): 
     442    for name, command in command.subCommands.items() \ 
     443        + command.aliasedSubCommands.items(): 
    418444        if name == 'shell': 
    419445            continue 
     
    422448        # add do command 
    423449        methodName = 'do_' + name 
    424         def generate(command): 
     450 
     451        def generateDo(command): 
     452 
    425453            def do_(s, line): 
    426454                # the do_ method is passed a single argument consisting of 
     
    430458                command.parse(args) 
    431459            return do_ 
    432         method = generate(command) 
     460 
     461        method = generateDo(command) 
    433462        setattr(cmdClass, methodName, method) 
     463 
    434464 
    435465        # add help command 
    436466        methodName = 'help_' + name 
    437         def generate(command): 
     467 
     468        def generateHelp(command): 
     469 
    438470            def help_(s): 
    439471                command.parser.print_help(file=command.stdout) 
    440472            return help_ 
    441         method = generate(command) 
     473 
     474        method = generateHelp(command) 
    442475        setattr(cmdClass, methodName, method) 
    443476 
Note: See TracChangeset for help on using the changeset viewer.