From f6e10c6c53668a5d9ae27444d8c2df3ba5afc49d Mon Sep 17 00:00:00 2001 From: Lily Chung Date: Sat, 11 Apr 2020 22:42:08 -0700 Subject: [PATCH] spire: implement "spire help [subcommand]" Add a "help" subcommand to any subcommand which takes subcommands. --- platform/spire/src/command.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/platform/spire/src/command.py b/platform/spire/src/command.py index 4625f4888..cad6c6fce 100644 --- a/platform/spire/src/command.py +++ b/platform/spire/src/command.py @@ -53,6 +53,26 @@ def configure(self, command: list, parser: argparse.ArgumentParser): except AttributeError as e: raise Exception("error configuring subcommand {!r}".format(subcommand)) from e + if 'help' not in self.mapping: + help_subparser = subparsers.add_parser( + 'help', + description='print help about this command or a subcommnad', + help='show this help message and exit') + help_subparser.add_argument('subcommand', nargs='?') + def help_function(aargs): + if aargs.subcommand is None: + parser.print_help() + else: + try: + subparser = subparsers.choices[aargs.subcommand] + except KeyError: + print('{}: no such subcommand'.format(aargs.subcommand)) + parser.print_help() + return + subparser.print_help() + help_subparser.set_defaults(argparse_invoke=help_function, + argparse_parser=help_subparser) + def short_doc(self): return self.__doc__