[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 <Bindhiya.KanangotBalakrishnan@amd.com>

[ROCm/amdsmi commit: 3681f900ee]
Este commit está contenido en:
Kanangot Balakrishnan, Bindhiya
2025-03-26 13:11:17 -05:00
cometido por GitHub
padre a735c401af
commit ec9be97b9f
Se han modificado 2 ficheros con 25 adiciones y 1 borrados
+10 -1
Ver fichero
@@ -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:
@@ -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__()