From f796972188a158bb5fe8fc5439ebacddbed41867 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 8 Sep 2015 18:44:16 +0200 Subject: Options: Add possibility to have interactive only Subcommands Some subcommands only makes sense, if they are called from an interactive shell and to interface a running server. Now we can mark a Subcommand as only_interactive and it will not be callable as argument from the command line. --- src/lib/Bcfg2/Options/Subcommands.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/lib/Bcfg2/Options/Subcommands.py b/src/lib/Bcfg2/Options/Subcommands.py index a9de738a4..a23537fc4 100644 --- a/src/lib/Bcfg2/Options/Subcommands.py +++ b/src/lib/Bcfg2/Options/Subcommands.py @@ -48,6 +48,10 @@ class Subcommand(object): #: one, ``bcfg2-admin`` does not.) interactive = True + #: Whether or not to expose this command as command line parameter + #: or only in an interactive :class:`cmd.Cmd` shell. + only_interactive = False + _ws_re = re.compile(r'\s+', flags=re.MULTILINE) def __init__(self): @@ -142,7 +146,9 @@ class Help(Subcommand): self._registry = registry def run(self, setup): - commands = self._registry.commands + commands = dict((name, cmd) + for (name, cmd) in self._registry.commands.items() + if not cmd.only_interactive) if setup.command: try: commands[setup.command].parser.print_help() @@ -210,10 +216,13 @@ class CommandRegistry(object): cmdcls = cmd_obj.__class__ name = cmdcls.__name__.lower() self.commands[name] = cmd_obj - # 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 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) -- cgit v1.2.3-1-g7c22