From 50b1e1f6a12fe90758977a228747a29860ea00c6 Mon Sep 17 00:00:00 2001 From: Gustavo Herzmann Date: Wed, 20 Sep 2023 14:17:06 -0300 Subject: [PATCH] Fix subcommand help and no-auth verification This commit fixes the -h/--help functionality for subcommands. Previously, using -h/--help with subcommands returned the help message for the base dcmanager command. Now, it correctly displays the help output specific to the subcommand, as if you had used 'dcmanager help '. Additionally, this commit improves the no-auth verification for help and bash completion commands, resolving an issue where the help command for subcommands required authentication. Test Plan: 1. PASS: Verify that the following commands still display the help message for the base dcmanager command: - dcmanager -h/--help - dcmanager help 2. PASS: Verify that the following commands correctly display the help message for the subcommand: - dcmanager subcloud list -h/--help - dcmanager help subcloud list 3. PASS: Without sourcing openrc, run the help and bash completion commands and confirm that they display the help message without requiring authentication. Closes-Bug: 2078944 Change-Id: I04dbf996f5c3efa570d4ed4841f526cfb15e02ff Signed-off-by: Gustavo Herzmann --- .../dcmanagerclient/shell.py | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/distributedcloud-client/dcmanagerclient/shell.py b/distributedcloud-client/dcmanagerclient/shell.py index b07b41e3..b57b6b99 100644 --- a/distributedcloud-client/dcmanagerclient/shell.py +++ b/distributedcloud-client/dcmanagerclient/shell.py @@ -151,6 +151,7 @@ class DCManagerShell(app.App): description=__doc__.strip(), version=dcmanager_version, command_manager=commandmanager.CommandManager("dcmanager.cli"), + deferred_help=True, ) # Override default help command @@ -228,14 +229,23 @@ class DCManagerShell(app.App): help="Suppress output except warnings and errors.", ) - parser.add_argument( - "-h", - "--help", - action=HelpAction, - nargs=0, - default=self, # tricky - help="Show this help message and exit.", - ) + if self.deferred_help: + parser.add_argument( + "-h", + "--help", + dest="deferred_help", + action="store_true", + help="Show help message and exit.", + ) + else: + parser.add_argument( + "-h", + "--help", + action=HelpAction, + nargs=0, + default=self, # tricky + help="Show help message and exit.", + ) parser.add_argument( "--debug", @@ -415,6 +425,11 @@ class DCManagerShell(app.App): return parser + def print_help_if_requested(self): + if self.deferred_help and self.options.deferred_help: + action = HelpAction(None, None, default=self) + action(self.parser, self.options, None, None) + def initialize_app(self, argv): self._clear_shell_commands() @@ -422,10 +437,13 @@ class DCManagerShell(app.App): self._set_shell_commands(self._get_commands(ver)) - do_help = ["help", "-h", "bash-completion", "complete"] + no_auth_commands = {"help", "bash-completion", "complete"} - # bash-completion should not require authentication. - skip_auth = "".join(argv) in do_help + # Skip authentication if the first argument is a no-auth command or + # if deferred_help is True + skip_auth = ( + argv[0] in no_auth_commands if argv else False + ) or self.options.deferred_help if skip_auth: self.options.auth_url = None