Skip to content

Commit ec9be97

Browse files
authored
[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: 3681f90]
1 parent a735c40 commit ec9be97

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

projects/amdsmi/amdsmi_cli/amdsmi_cli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,18 @@ def _print_error(e, destination):
103103
except NameError:
104104
logging.debug("argcomplete module not found. Autocomplete will not work.")
105105

106+
valid_commands = ['version', 'list', 'static', 'firmware', 'bad-pages',
107+
'metric', 'process', 'profile', 'event', 'topology', 'set',
108+
'reset', 'monitor', 'xgmi', 'partition', '--help']
109+
106110
sys.argv = [arg.lower() if arg.startswith('--') or not arg.startswith('-')
107111
else arg for arg in sys.argv]
108-
args = amd_smi_parser.parse_args(args=None if sys.argv[1:] else ['--help'])
112+
if len(sys.argv) == 1:
113+
args = amd_smi_parser.parse_args(args=['--help'])
114+
elif sys.argv[1] in valid_commands:
115+
args = amd_smi_parser.parse_args(args=None)
116+
else:
117+
raise amdsmi_cli_exceptions.AmdSmiInvalidSubcommandException(sys.argv[1],amd_smi_commands.logger.destination)
109118

110119
# Handle command modifiers before subcommand execution
111120
if args.json:

projects/amdsmi/amdsmi_cli/amdsmi_cli_exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ def __init__(self, command, outputformat: str):
241241
self.stdout_message = f"{common_message} Error code: {self.value}"
242242

243243

244+
class AmdSmiInvalidSubcommandException(AmdSmiException):
245+
def __init__(self, command, outputformat: str):
246+
super().__init__()
247+
self.value = -10
248+
self.command = command
249+
self.output_format = outputformat
250+
251+
common_message = f"AMD-SMI Command '{self.command}' is invalid. Must receive valid AMD-SMI Command first. Run '--help' for more info."
252+
253+
self.json_message["error"] = common_message
254+
self.json_message["code"] = self.value
255+
self.csv_message = f"error,code\n{common_message}, {self.value}"
256+
self.stdout_message = f"{common_message} Error code: {self.value}"
257+
258+
244259
class AmdSmiUnknownErrorException(AmdSmiException):
245260
def __init__(self, command, outputformat: str):
246261
super().__init__()

0 commit comments

Comments
 (0)