8 Commits
Author SHA1 Message Date
Ross Goldberg 157750ee99 Prevent bash option reversion commands in bash completion function from being appended to bash history. (#859)
Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2026-03-20 12:27:46 -05:00
Ross Goldberg 6d473eaa0a Fix generated shell completion script handling of repeating & non-repeating positional arguments, flags & options (#808)
* Complete repeatable positional instances after the first in bash.
* Complete repeatable positional instances after the first in fish.
* Only complete positionals through the first repeating positional in zsh.
* Complete repeatable flag & option instances after the first in bash.
* Complete non-repeatable flag & option instances only once in fish.
2025-12-17 14:41:19 +00:00
Ross Goldberg 04695ec544 Escape single quotes in shellCommand completion for fish. (#811)
Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-09-19 12:39:07 -05:00
Ross Goldberg 932e6cdd2a Refactor completion script generation to use ToolInfoV0 (#764)
* Nonexclusive flags implemented via an array of enum cases are now separate ArgumentInfoV0 instances, instead of different names for the same ArgumentInfoV0.
* Improve ToolInfoV0 HelpCommand injection.
* Add ArgumentInfoV0.ParsingStrategyV0 enum.
* Refactor bash completions to use ToolInfoV0.
* Refactor fish completions to use ToolInfoV0.
* Refactor zsh completions to use ToolInfoV0.
* Remove vestigial shellVariableNamePrefix.
* Add .editorconfig files to prevent automatic whitespace changes to test snapshots.

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-06-08 15:44:34 -05:00
Ross Goldberg d8a9695190 Improve flag & option name quoting in completion generation for all 3 shells. (#767)
Improve zsh quoting.

Many quoting issues remain.

To fix them, there should first be enforced & documented limits on the acceptable characters in various values throughout SAP to avoid unnecessary quoting.

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-05-19 18:24:11 -05:00
Ross Goldberg 42585ad612 Add 2 index arguments to custom shell completion calls (#763)
They indicate to the Swift custom completion function:

1. the word for which completions are being requested.
2. the location of the cursor within that word.
2025-05-07 16:32:11 -05:00
Ross Goldberg bdb3b27a68 Improve bash completion script generation (#735)
* Restrict access to symbols in BashCompletionsGenerator.swift.

* Use key path instead of closure in BashCompletionsGenerator.swift.

* Remove extraneous bash spacing.

* Do not indent bash cases.

Standardize bash indents.

* Do not prefix bash cases with an open parenthesis.

* Brace & quote bash variable uses.

* Remove extraneous bash blank line.

* Improve bash escaping.

* Improve bash $cur, $prev, & $COMPREPLY.

Use positional arguments passed by bash to the main completion function instead of reading from COMP_WORDS, as that can return the wrong info if completing an empty word before a non-empty word.

Make $cur & $prev local & readonly.

Remove unnecessary COMPREPLY=().

* Improve bash $SAP_SHELL & $SAP_SHELL_VERSION.

Make them local & readonly.

* Overhaul BashCompletionsGenerator.swift as [ParsableCommand.Type] extension.

Inline some single-use functions.

* Move bashValueCompletion(…) in BashCompletionsGenerator.swift.

Move from ArgumentDefinition extension to [ParsableCommand.Type] extension.

* Overhaul bash completion script generation:

Attempt to emulate the much more complete & correct zsh completion script.

Offer candidates for only the current positional / option value, not for all.

Generate completions for positionals the same as for option values.

Offer flags & options only if no prior option-terminator marker (a standalone --).

Offer flags & options only if current word starts with a -, or if there are no remaining positional parameters.

Parse options prior to subcommands later in the command line.

Offer flags & options only once.

Do not offer flags or options if the current word is an option value.

* Add default help to bash completions iff no existing help subcommand.

* Improve bash file & directory completions.

Do not split paths with spaces into separate completions.

Escape spaces in paths.

Append / to directory paths.

Do not use _filedir from bash-completions; use builtin bash constructs instead.

Fix broken existing escaping of single quotes in file extension filters.

More succinct & performant.

* Disable history ! in bash completion scripts.

* Do not include uppercased extensions in bash file(extensions:) completions.

That behavior was a bug, not a feature.

Released Swift Argument Parser documentation says:

"Complete file names with the specified extensions."

It does not mention including uppercase versions of the extensions.

None of the other shells include uppercase versions of extensions.

Why should uppercase versions be special? Why not case-insensitive matching? Why not lowercase versions? Etc.

Any config depending on this behavior won't match uppercase extensions without manually being reconfigured, but there are a ton of other bug fixes that can also break compatibility.

* Use single quoted string for bash list completions.

bash scripts now escape single quotes in list values.

Any existing list values with escapes that worked in double quotes will not work in the single quotes. But now:

- list values needn't be escaped
- unescaped double quotes in list values won't break the script
- $ & other characters won't interact with the shell, so, e.g., command substitutions cannot cause problems

* Allow bash list completions to contain spaces.

* Allow bash custom completions to contain spaces.

Properly refuses to complete when there are no completion candidates.

Still cannot complete to an empty string.

* bash custom completion of empty word followed by other words.

* Prevent bash shellCommand completion scripting from breaking scripts.

Eval the given command from a single-quoted string instead of running it directly in the shell.

* Allow bash shellCommand completions to contain spaces.

If existing shellCommand completions depend on spaces as completion delimiters, they will not work anymore. Newlines are now the only supported delimiters.

Resolve #734

* Use zip(…) to generate bash positional argument numbers.

* Rework bash positional case generation.

* Reverse the polarity of the neutron flow of the completions help subcommand detector.

* Rename flags & options vars to flagCompletions & optionCompletions, respectively, in BashCompletionsGenerator.swift.

* Remove TODO comments from generated bash completion scripts.

---------

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-02-16 13:06:01 -06:00
Rauhul Varma d3630e3190 Move additional tests to snapshots (#700)
Moves some examples test to use snapshot files instead of inline
multiline strings.
2025-02-06 10:28:22 -08:00