From 3183714caadc5e5b896c580ec898e615875dc10a Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 13 Oct 2015 16:57:54 +0200 Subject: Options: Add possibility to have aliases for commands You can add now a list of aliases to a Subcommand to make it available with different names. Each alias in the list is used without modification, especially the aliases do not get converted to lower case (this is for example required for "EOF"). --- src/lib/Bcfg2/Options/Subcommands.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/lib/Bcfg2/Options/Subcommands.py b/src/lib/Bcfg2/Options/Subcommands.py index a23537fc4..2ba81e18d 100644 --- a/src/lib/Bcfg2/Options/Subcommands.py +++ b/src/lib/Bcfg2/Options/Subcommands.py @@ -52,6 +52,10 @@ class Subcommand(object): #: or only in an interactive :class:`cmd.Cmd` shell. only_interactive = False + #: Additional aliases for the command. The contents of the list gets + #: added to the default command name (the lowercased class name) + aliases = [] + _ws_re = re.compile(r'\s+', flags=re.MULTILINE) def __init__(self): @@ -214,18 +218,22 @@ class CommandRegistry(object): else: cmd_obj = cls_or_obj cmdcls = cmd_obj.__class__ - name = cmdcls.__name__.lower() - self.commands[name] = cmd_obj - - if not cmdcls.only_interactive: - # py2.5 can't mix *magic and non-magical keyword args, thus - # the **dict(...) - self.subcommand_options.append( - Subparser(*cmdcls.options, **dict(name=name, - help=cmdcls.__doc__))) - if issubclass(self.__class__, cmd.Cmd) and cmdcls.interactive: - setattr(self, "do_%s" % name, cmd_obj) - setattr(self, "help_%s" % name, cmd_obj.parser.print_help) + names = [cmdcls.__name__.lower()] + if cmdcls.aliases: + names.extend(cmdcls.aliases) + + for name in names: + self.commands[name] = cmd_obj + + if not cmdcls.only_interactive: + # py2.5 can't mix *magic and non-magical keyword args, thus + # the **dict(...) + self.subcommand_options.append( + Subparser(*cmdcls.options, **dict(name=name, + help=cmdcls.__doc__))) + if issubclass(self.__class__, cmd.Cmd) and cmdcls.interactive: + setattr(self, "do_%s" % name, cmd_obj) + setattr(self, "help_%s" % name, cmd_obj.parser.print_help) return cmd_obj def register_commands(self, candidates, parent=Subcommand): -- cgit v1.2.3-1-g7c22