Commit graph

21 commits

Author SHA1 Message Date
Marc Khouzam
95d23d24ff
Fix zsh for DirectiveNoSpace and DirectiveNoFileComp (#1213)
Fixes #1211

When handling ShellCompDirectiveNoSpace we must still properly handle
descriptions.  To do so we cannot simply use 'compadd', but must use
zsh's '_describe' function.

Also, when handling ShellCompDirectiveNoSpace we cannot assume that
only a single completion will be given to the script.  In fact,
ValidArgsFunction can return multiple completions, even if they don't
match the 'toComplete' argument prefix.  Therefore, we cannot use the
number of completions received in the completion script to determine
if we should activate the "no space" directive.  Instead, we can leave
it all to the '_describe' function.

Fixes #1212

When handling ShellCompDirectiveNoFileComp we cannot base ourself on
the script receiving no valid completion. In fact,
ValidArgsFunction can return multiple completions, even if they don't
match the 'toComplete' argument prefix at all.  Therefore, we cannot use
the number of completions received by the completion script to determine
if we should activate the "no file comp" directive.  Instead, we can
check if the '_describe' function has found any completions.

Finally, it is important for the script to return the return code of the
called zsh functions (_describe, _arguments).  This tells zsh if
completions were found or not, which if not, will trigger different
matching attempts, such as matching what the user typed with the the
content of possible completions (instead of just as the prefix).

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2021-05-03 10:54:00 -06:00
Unai Martinez-Corral
652c755d37
Use golangci-lint (#1044)
Use golangci-lint. Repair warnings and errors resulting from linting.
2021-02-07 17:08:50 -07:00
midchildan
f64bfa1e08
Fix zsh completion not working on the first time in a shell session (#1237)
The zsh completion script output by cobra is a stub completion function
which replaces itself with the actual completion function. This
technique enables cobra to define helper functions without splitting the
completion script into multiple files.  However, the current
implementation forgets to call the actual completion function at the end
of the stub function, meaning that completion won't work the first time
it's invoked in a shell session. This commit is a fix for this problem.
2020-10-03 19:25:07 -06:00
Marc Khouzam
2c5a0d300f
Extend Go completions and revamp zsh comp (#1070) (#1070)
Replace the current Zsh completion with a Zsh completion solution based
on Go completions. This allows to support custom completions (based
on Go completions), but also to standardize the behavior of completion
across all shells.

Also, add support to Go completions for the bash completion annotations:
  BashCompFilenameExt (including Command.MarkFlagFilename() family)
                       - still supported by zsh
  BashCompSubdirsInDir - now supported by zsh
  BashCompOneRequiredFlag (including Command.MarkFlagRequired() family)
                       - now supported by zsh and fish

Finally, remove the suggestin of the = form of flag completion.
The = form is supported, but it will not be suggested to avoid having
duplicated suggestions.
2020-06-29 13:52:14 -06:00
Haim Ashkenazi
e2c45ac9eb Started working on Unified API for the various shell completions:
- Moved some general function to a more generic shell_completions file.
- Added functions to mark flag as directory completion.
- Started making the global functions docs more generic (not bash
  specific) and added compatibility matrix.
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
edbb6712e2 zsh-completions: implemented argument completion. 2019-06-07 10:09:50 -04:00
Haim Ashkenazi
d262154093 zsh-completion: tidy up function and variable names
There are many files in the package, renamed all zsh-completion
related names to convey that.
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
8822449c0f zsh-completion: added escapinng of single quotes in flag description. 2019-06-07 10:09:50 -04:00
Haim Ashkenazi
7ce08e227e zsh-completion: completion should always parse the root command!
It was running on the command it was invoked from which caused some
additional helpers (--help, --version) not to be generated.
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
50f385938e zsh-completion: added support for filename globbing. 2019-06-07 10:09:50 -04:00
Haim Ashkenazi
461a39d5b9 zsh-completion: removed forgotten function. 2019-06-07 10:09:50 -04:00
Haim Ashkenazi
df12a0a249 zsh-completion: two fixes for identifying flag usage:
Fixed after input from @eparis:
- Decide on option parameter by checking NoOptDefVal
- Slices also could be specified multiple times.
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
ec4b8c974c zsh-completions: revised flags completion rendering + new features:
- If the flags are not bool the completion expects argument.
- You don't have to specify file extensions for file completion to
  work.
- Allow multiple occurrences of flag if type is stringArray.

Need to verify that these assumption are correct :)
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
e8018e8612 zsh-completion template refactoring:
- removed redundant function
- improved other functions :)
- better names for other functions
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
2662787697 zsh-completion: added support for subcommand description.
Also make the template more elegant on the way...
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
f0508c8e76 zsh-completion ignores hidden commands and flags :) 2019-06-07 10:09:50 -04:00
Haim Ashkenazi
a15d099018 zsh-completion fixed reference to cmd name
cmd.Use is not the command name :). Found it once I figured out
that I need to execute the command in order to fully test the
generated completion.
2019-06-07 10:09:50 -04:00
Haim Ashkenazi
7e2436b79d First try at better zsh completions:
A very basic POC. Need to refactor to generate completion
structure before passing to the template to avoid repeated
computations.

What works:
  * Real zsh completion (not built on bash)
  * Basic flags (with long flag and optional shorthand)
  * Basic filename completion indication (not with file extensions though)

What's missing:
  * File extensions to filename completions
  * Positional args
  * Do we require handling only short flags?
2019-06-07 10:09:50 -04:00
Eric Paris
b787445794 Use func (c *Command) consistently (#530)
It makes the docs looks better. The idea was suggested by @SamWhited
2017-09-05 13:20:51 -04:00
agmen
4de692c1eb adding a GenZshCompletionFile function to mirror bash_completion (#529) 2017-09-05 12:32:32 -04:00
bpicode
d7ba19510d Add basic zsh completion (command hierarchy only)
Partially fixes #107
See PR #497
2017-07-30 02:44:28 -06:00