From 7e7eb69edffca39b51e8fdcd5eaa76262e16eaf4 Mon Sep 17 00:00:00 2001 From: Erovia Date: Thu, 21 Nov 2019 21:52:00 +0100 Subject: [PATCH] MILC: Add support for hidden subcommands (#7034) * MILC: Add support for hidden subcommands Subcommands with 'hidden=True' will not show up in the help output, but will work as any other subcommands. * Hide those hidden submodules, for real now * Rebase on latest MILC --- lib/python/milc.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/python/milc.py b/lib/python/milc.py index e8599eff3f..4392c8376a 100644 --- a/lib/python/milc.py +++ b/lib/python/milc.py @@ -595,23 +595,25 @@ class MILC(object): return entrypoint_func - def add_subcommand(self, handler, description, name=None, **kwargs): + def add_subcommand(self, handler, description, name=None, hidden=False, **kwargs): """Register a subcommand. If name is not provided we use `handler.__name__`. """ + if self._inside_context_manager: raise RuntimeError('You must run this before the with statement!') if self._subparsers is None: - self.add_subparsers() + self.add_subparsers(metavar="") if not name: name = handler.__name__.replace("_", "-") self.acquire_lock() - - kwargs['help'] = description + if not hidden: + self._subparsers.metavar = "{%s,%s}" % (self._subparsers.metavar[1:-1], name) if self._subparsers.metavar else "{%s%s}" % (self._subparsers.metavar[1:-1], name) + kwargs['help'] = description self.subcommands[name] = SubparserWrapper(self, name, self._subparsers.add_parser(name, **kwargs)) self.subcommands[name].set_defaults(entrypoint=handler) @@ -619,11 +621,11 @@ class MILC(object): return handler - def subcommand(self, description, **kwargs): + def subcommand(self, description, hidden=False, **kwargs): """Decorator to register a subcommand. """ def subcommand_function(handler): - return self.add_subcommand(handler, description, **kwargs) + return self.add_subcommand(handler, description, hidden=hidden, **kwargs) return subcommand_function