Skip to content

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

Merged
natecook1000 merged 4 commits into
apple:mainfrom
mayoff:zsh-completion-repeated-option
Feb 24, 2024
Merged

Improve zsh completion for repeatable options (#564)#614
natecook1000 merged 4 commits into
apple:mainfrom
mayoff:zsh-completion-repeated-option

Conversation

@mayoff
Copy link
Copy Markdown
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

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
Copy Markdown
Member

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

@mayoff
Copy link
Copy Markdown
Contributor Author

mayoff commented Jan 29, 2024

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

@natecook1000 natecook1000 force-pushed the zsh-completion-repeated-option branch from bf8ca59 to c2f62e5 Compare February 7, 2024 06:13
@natecook1000 natecook1000 force-pushed the zsh-completion-repeated-option branch from c2f62e5 to 8bcfbc6 Compare February 7, 2024 06:16
@natecook1000
Copy link
Copy Markdown
Member

@swift-ci Please test

@natecook1000 natecook1000 enabled auto-merge (squash) February 24, 2024 19:33
@natecook1000
Copy link
Copy Markdown
Member

@swift-ci Please test

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