Skip to content

Fix: external_subcommand does not 'tab complete' in ZSH#6027

Merged
epage merged 2 commits intoclap-rs:masterfrom
Alpha1337k:master
Jun 9, 2025
Merged

Fix: external_subcommand does not 'tab complete' in ZSH#6027
epage merged 2 commits intoclap-rs:masterfrom
Alpha1337k:master

Conversation

@Alpha1337k
Copy link
Contributor

Proposal for #6016.

Same fix is applied as in the linked issue. Added zsh only test since the issue only persists on zsh.

This fix allows default completions on external commands. Right now the completions are disabled, making for an annoying DX. The behaviour is now in line with bash, fish.

@epage
Copy link
Member

epage commented Jun 9, 2025

Sorry, forgot to call this out in the last round: I asked for more details on a solution in the issue

. I'm in a bad spot for evaluating a suggestion or implementing a fix. If someone wants to propose something and provide the background for why it is the right call, I'd be willing to accept it.

@Alpha1337k
Copy link
Contributor Author

The issue lays in the fact that the completions only consider specified options:

...
'-h[Print help]' \
'--help[Print help]' \
...

Any other option will not get tab-completed. For external subcommands, the options are inherently not specified. But by only having specified variables in the completions the tab complete does not function.

The fix is to allow the default zsh autocomplete for other variables. So - <TAB> will still suggest -h and --help, but when prompted with something like ./src it will suggest files in that directory. This issue is seen in uv run for example.

This would be added to the completions by the pr, fixing the issue:

...
'*::external_command:_default' \
...

explanation by GPT-4.1:

This is a completion specification. Here’s what it means:
The asterisk (*) indicates that this completion rule applies to any number of remaining arguments on the command line.
The double colon (::) separates the argument pattern from the action to take.
external_command is a label or description for this argument group, which may be shown in the completion menu.
_default is the Zsh function that will be called to generate completions for these arguments. _default is a built-in function that provides generic filename completion.

I've tested the change by running zsh in a docker container, and found the solution to be working perfectly, if needed you can try with the following commands:

cp clap_complete/tests/snapshots/external_subcommands.zsh /usr/local/share/zsh/site-functions/_my-app

compinit

my-app <TAB>

Anything in particular that needs to be explained further?

@epage
Copy link
Member

epage commented Jun 9, 2025

As I'm not in a position to evaluate the accuracy of GPT derived responses, I would ask that you refrain from using them.

I'd like to better understand the significance of the external;_command label. What is the impact of setting it? Is there any side effects of its use?

@Alpha1337k
Copy link
Contributor Author

By setting external_command, it can be displayed as a placeholder / suggestion when ctrl-D is pressed:

my-app external <CTRL-D>                                                                                                                                                                                               
[external_command]
CHANGELOG.md            Cargo.lock              LICENSE-MIT

Other than that I have not seen the tag used in ZSH.

@epage
Copy link
Member

epage commented Jun 9, 2025

Thanks!

@epage epage merged commit 0f59d55 into clap-rs:master Jun 9, 2025
25 checks passed
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