From eca6a01008ddbc31ab5719a0321ecd8c9e925e68 Mon Sep 17 00:00:00 2001 From: Chris Lumens Date: Thu, 28 Jun 2018 10:07:33 -0400 Subject: [PATCH] If the help subcommand is given, print the help output. This isn't a real subcommand like the others. The option processing just intercepts it and prints the output. Given that we're subcommand based, it makes sense to support this in addition to --help. (cherry picked from commit 18620700fdc6b9d6315857a76693231247d25c81) --- src/bin/composer-cli | 13 ++++++++++++- src/composer/cli/__init__.py | 11 ++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/bin/composer-cli b/src/bin/composer-cli index dc1f65ec..56a9c4cc 100755 --- a/src/bin/composer-cli +++ b/src/bin/composer-cli @@ -47,7 +47,8 @@ def setup_logging(logfile): if __name__ == '__main__': # parse the arguments - opts = composer_cli_parser().parse_args() + arg_parser = composer_cli_parser() + opts = arg_parser.parse_args() if opts.showver: print(VERSION) @@ -59,6 +60,16 @@ if __name__ == '__main__': setup_logging(opts.logfile) log.debug("opts=%s", opts) + if len(opts.args) == 0: + log.error("Missing command") + sys.exit(1) + elif opts.args[0] == "help": + arg_parser.print_help() + sys.exit(0) + elif len(opts.args) == 1: + log.error("Missing %s sub-command", opts.args[0]) + sys.exit(1) + errors = [] # Check to see if the socket exists and can be accessed diff --git a/src/composer/cli/__init__.py b/src/composer/cli/__init__.py index d313067f..eab53828 100644 --- a/src/composer/cli/__init__.py +++ b/src/composer/cli/__init__.py @@ -40,15 +40,12 @@ def main(opts): :param opts: Cmdline arguments :type opts: argparse.Namespace """ - if len(opts.args) == 0: - log.error("Missing command") - return 1 - elif opts.args[0] not in command_map: + + # Making sure opts.args is not empty (thus, has a command and subcommand) + # is already handled in src/bin/composer-cli. + if opts.args[0] not in command_map: log.error("Unknown command %s", opts.args[0]) return 1 - if len(opts.args) == 1: - log.error("Missing %s sub-command", opts.args[0]) - return 1 else: try: return command_map[opts.args[0]](opts)