Fixed show vxlan remotemac ambiguity #4121
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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