diff options
author | Alexander Sulfrian <alexander.sulfrian@fu-berlin.de> | 2015-10-13 16:57:54 +0200 |
---|---|---|
committer | Sol Jerome <sol.jerome@gmail.com> | 2015-10-13 13:11:02 -0500 |
commit | 3183714caadc5e5b896c580ec898e615875dc10a (patch) | |
tree | ef7c7fc477a514bab8421b00921e50e12a756df2 /src/lib/Bcfg2 | |
parent | ed93977a0d476105bb74600af0ff4954aa248c28 (diff) | |
download | bcfg2-3183714caadc5e5b896c580ec898e615875dc10a.tar.gz bcfg2-3183714caadc5e5b896c580ec898e615875dc10a.tar.bz2 bcfg2-3183714caadc5e5b896c580ec898e615875dc10a.zip |
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").
Diffstat (limited to 'src/lib/Bcfg2')
-rw-r--r-- | src/lib/Bcfg2/Options/Subcommands.py | 32 |
1 files 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): |