Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve zsh completion for repeatable options (#564) #614

Merged
merged 4 commits into from
Feb 24, 2024

Conversation

mayoff
Copy link
Contributor

@mayoff mayoff commented Jan 10, 2024

Some commands allow an option to be repeated to provide multiple values. For example, ssh allows the -L flag to be repeated to establish multiple port forwardings.

ArgumentParser supports this style when the Option value is an Array and the parsingStrategy is ArrayParsingStrategy.singleValue or .unconditionalSingleValue.

Without this patch, ArgumentParser generates a zsh completion script that does not handle repeatable options correctly. The generated script suppresses completion of any option after that option's first use, even if the option is repeatable.

With this patch, ArgumentParser generates a zsh completion script that allows a repeatable option to be completed each time it is used.

The relevant zsh _arguments syntax is documented here: https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-Functions

Specifically, a repeatable option's optspec needs to start with '*'. Furthermore, a repeatable argument must not list itself or its synonyms in its own parenthesized suppression list. (This is not clearly documented.)

fixes #564

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Some commands allow an option to be repeated to provide multiple values.
For example, ssh allows the -L flag to be repeated to establish multiple
port forwardings.

ArgumentParser supports this style when the Option value is an Array
and the parsingStrategy is ArrayParsingStrategy.singleValue or
.unconditionalSingleValue.

Without this patch, ArgumentParser generates a zsh completion script
that does not handle repeatable options correctly. The generated script
suppresses completion of any option after that option's first use, even
if the option is repeatable.

With this patch, ArgumentParser generates a zsh completion script that
allows a repeatable option to be completed each time it is used.

The relevant zsh _arguments syntax is documented here:
https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-Functions

Specifically, a repeatable option's optspec needs to start with '*'.
Furthermore, a repeatable argument must not list itself or its synonyms
in its own parenthesized suppression list. (This is not clearly
documented.)

fixes apple#564
@mayoff mayoff force-pushed the zsh-completion-repeated-option branch from afe4e36 to 5e24139 Compare January 10, 2024 21:36
@natecook1000
Copy link
Member

Thanks @mayoff! Do we need the same fix for repeatable flags?

@mayoff
Copy link
Contributor Author

mayoff commented Jan 29, 2024

This patch also makes a repeatable @Flag complete correctly in zsh.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@natecook1000 natecook1000 force-pushed the zsh-completion-repeated-option branch from bf8ca59 to c2f62e5 Compare February 7, 2024 06:13

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@natecook1000 natecook1000 force-pushed the zsh-completion-repeated-option branch from c2f62e5 to 8bcfbc6 Compare February 7, 2024 06:16
@natecook1000
Copy link
Member

@swift-ci Please test

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@natecook1000 natecook1000 enabled auto-merge (squash) February 24, 2024 19:33
@natecook1000
Copy link
Member

@swift-ci Please test

@natecook1000 natecook1000 merged commit 76466cc into apple:main Feb 24, 2024
2 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.

Zsh completion script only shows a completion for the first time in an array.
2 participants