Skip to content

Conversation

@Gnanapriya27
Copy link

@Gnanapriya27 Gnanapriya27 commented Nov 10, 2025

Summary:
When users type ambiguous commands like show vxlan remote, they see a Python traceback instead of a clean error message because the CLI finds multiple matching commands and throws an backtrace and an exception.

Root Cause
The AliasedGroup.get_command() method calls ctx.fail() which raises a UsageError exception that propagates through Click's bash completion system, causing the traceback.

Approach:
Implement context-aware error handling in the [AliasedGroup.get_command() method to differentiate between: Bash completion context: Where tracebacks should be suppressed Normal command execution context: Where clean error messages should be displayed

How did you do it?
Added environment variable detection to handle bash completion context differently Command execution results in a clear error message without any tracebacks No changes to the existing CLI functionality

How to verify it

sonic-utility UT - Ran UT cases and results are intact

Previous command output (if the output of a command-line utility has changed)

root@W65-TL7-DUT-1:/home/admin# show vxlan remote
Traceback (most recent call last):
File "/usr/local/bin/show", line 8, in
sys.exit(cli())
^^^^^
File "/usr/local/lib/python3.11/dist-packages/click/core.py", line 764, in call
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/click/core.py", line 712, in main
_bashcomplete(self, prog_name, complete_var)
File "/usr/local/lib/python3.11/dist-packages/click/core.py", line 57, in _bashcomplete
if bashcomplete(cmd, prog_name, complete_var, complete_instr):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/click/_bashcomplete.py", line 292, in bashcomplete
return do_complete(cli, prog_name, complete_instr == 'complete_zsh')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/click/_bashcomplete.py", line 277, in do_complete
for item in get_choices(cli, prog_name, args, incomplete):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/click/_bashcomplete.py", line 232, in get_choices
ctx = resolve_ctx(cli, prog_name, args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/click/_bashcomplete.py", line 98, in resolve_ctx
cmd_name, cmd, args = ctx.command.resolve_command(ctx, args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/click/core.py", line 1171, in resolve_command
cmd = self.get_command(ctx, cmd_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/utilities_common/cli.py", line 119, in get_command
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))
File "/usr/local/lib/python3.11/dist-packages/click/core.py", line 496, in fail
raise UsageError(message, self)
click.exceptions.UsageError: Too many matches: remotemac, remotevni, remotevtepUsage: show vxlan [OPTIONS] COMMAND [ARGS]...
Try "show vxlan -h" for help.Error: Too many matches: remotemac, remotevni, remotevtep

New command output (if the output of a command-line utility has changed)

root@W65-TL7-DUT-1:/home/admin# show vxlan remote
remotemac remotevni remotevtep

root@W65-TL7-DUT-1:/home/admin# show vxlan remote mac
Usage: show vxlan [OPTIONS] COMMAND [ARGS]...
Try "show vxlan -h" for help.

Error: Too many matches: remotemac, remotevni, remotevtep

Summary:
When users type ambiguous commands like show vxlan remote, they see a Python traceback instead of a clean error message because the CLI finds multiple matching commands and throws an backtrace and an exception.

Root Cause
The AliasedGroup.get_command() method calls ctx.fail() which raises a UsageError exception that propagates through Click's bash completion system, causing the traceback.

Approach:
Implement context-aware error handling in the [AliasedGroup.get_command() method to differentiate between:
Bash completion context: Where tracebacks should be suppressed
Normal command execution context: Where clean error messages should be displayed

How did you do it?
Added environment variable detection to handle bash completion context differently
Command execution results in a clear error message without any tracebacks
No changes to the existing CLI functionality

Signed-off-by: Gnanapriya Sethuramarajan <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants