From ec9be97b9f2568cc05e60edc5db2ef6b4a9691bf Mon Sep 17 00:00:00 2001 From: "Kanangot Balakrishnan, Bindhiya" Date: Wed, 26 Mar 2025 13:11:17 -0500 Subject: [PATCH] [SWDEV-513958] Fix error message due to argparse behavior (#108) When argparse parses multiple invalid arguments, the error message displays only the last argument and this leads to confusion. To avoid the scenario, added valid command check before argparse and in case of invalid first command, added new exception. Signed-off-by: Bindhiya Kanangot Balakrishnan [ROCm/amdsmi commit: 3681f900ee9a7b31d469f808c8f5b66f88d2a920] --- projects/amdsmi/amdsmi_cli/amdsmi_cli.py | 11 ++++++++++- .../amdsmi/amdsmi_cli/amdsmi_cli_exceptions.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/projects/amdsmi/amdsmi_cli/amdsmi_cli.py b/projects/amdsmi/amdsmi_cli/amdsmi_cli.py index 8b88025d46..d104745e85 100755 --- a/projects/amdsmi/amdsmi_cli/amdsmi_cli.py +++ b/projects/amdsmi/amdsmi_cli/amdsmi_cli.py @@ -103,9 +103,18 @@ if __name__ == "__main__": except NameError: logging.debug("argcomplete module not found. Autocomplete will not work.") + valid_commands = ['version', 'list', 'static', 'firmware', 'bad-pages', + 'metric', 'process', 'profile', 'event', 'topology', 'set', + 'reset', 'monitor', 'xgmi', 'partition', '--help'] + sys.argv = [arg.lower() if arg.startswith('--') or not arg.startswith('-') else arg for arg in sys.argv] - args = amd_smi_parser.parse_args(args=None if sys.argv[1:] else ['--help']) + if len(sys.argv) == 1: + args = amd_smi_parser.parse_args(args=['--help']) + elif sys.argv[1] in valid_commands: + args = amd_smi_parser.parse_args(args=None) + else: + raise amdsmi_cli_exceptions.AmdSmiInvalidSubcommandException(sys.argv[1],amd_smi_commands.logger.destination) # Handle command modifiers before subcommand execution if args.json: diff --git a/projects/amdsmi/amdsmi_cli/amdsmi_cli_exceptions.py b/projects/amdsmi/amdsmi_cli/amdsmi_cli_exceptions.py index ad4ae386fc..ee4e823cb5 100644 --- a/projects/amdsmi/amdsmi_cli/amdsmi_cli_exceptions.py +++ b/projects/amdsmi/amdsmi_cli/amdsmi_cli_exceptions.py @@ -241,6 +241,21 @@ class AmdSmiRequiredCommandException(AmdSmiException): self.stdout_message = f"{common_message} Error code: {self.value}" +class AmdSmiInvalidSubcommandException(AmdSmiException): + def __init__(self, command, outputformat: str): + super().__init__() + self.value = -10 + self.command = command + self.output_format = outputformat + + common_message = f"AMD-SMI Command '{self.command}' is invalid. Must receive valid AMD-SMI Command first. Run '--help' for more info." + + self.json_message["error"] = common_message + self.json_message["code"] = self.value + self.csv_message = f"error,code\n{common_message}, {self.value}" + self.stdout_message = f"{common_message} Error code: {self.value}" + + class AmdSmiUnknownErrorException(AmdSmiException): def __init__(self, command, outputformat: str): super().__init__()