apple / swift-argument-parser

Straightforward, type-safe argument parsing for Swift
Apache License 2.0
3.3k stars 311 forks source link

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

Closed mayoff closed 6 months ago

mayoff commented 7 months ago

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

natecook1000 commented 7 months ago

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

mayoff commented 7 months ago

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

natecook1000 commented 6 months ago

@swift-ci Please test

natecook1000 commented 6 months ago

@swift-ci Please test