* Fix flag completion
The flag completion functions should not be stored in the root cmd.
There is no requirement that the root cmd should be the same when
`RegisterFlagCompletionFunc` was called. Storing the flags there does
not work when you add the the flags to your cmd struct before you add the
cmd to the parent/root cmd. The flags can no longer be found in the rigth
place when the completion command is called and thus the flag completion
does not work.
Also #1423 claims that this would be thread safe but we still have a map
which will fail when accessed concurrently. To truly fix this issue use a
RWMutex.
Fixes#1437Fixes#1320
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
* Fix trailing whitespaces in fish comp scripts
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
zsh is now the default on Mac OS, but the $_fpath version of the installation instructions are likely to put the completion in a strange location that the user might not expect (e.g. an oh-my-zsh plugin's function directory). So, since Mac OS seems to (as far as I can tell) provide a stable location, this PR recommends using that path instead.
If the interspersed option is set false and one arg is already set all
following arguments are counted as arg and not parsed as flags. Because
of that we should not offer flag completion. The same applies to arguments
followed after `--`.
Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
* Bash completion v2
This v2 version of bash completion is based on Go completions.
It also supports descriptions like the other shells.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Only consider matching completions for formatting
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Use bash compV2 for the default completion command
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Update comments that still referred to bash completion
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Fix fish for ShellDirectiveNoSpace and file comp
For fish shell we achieve ShellDirectiveNoSpace by outputing a fake
second completion with an extra character. However, this extra
character was being added after the description string, instead of
before. This commit fixes that.
It also cleans up the script of useless code, now that fish completion
details are better understood.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Handle case when completion starts with a space
Fixes#1303
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Support fish completion with env vars in the path
Fixes https://github.com/spf13/cobra/issues/1214
Fixes https://github.com/spf13/cobra/issues/1306
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Update based on review
1- We use `set -l` for local variable to make sure there are no
conflicts with global variables
2- We use `commandline -opc` which:
a) splits the command line into tokens (-o)
b) only considers the current command (-p) (e.g., echo hello; helm <TAB>)
c) stops at the cursor (-c)
3- We extract the last arg with `commandline -ct` and escape it to handle
the case where it is a space, or unmatched quote.
4- We avoid looping when filtering on prefix.
5- We don't add a fake comp for ShellCompDirectiveNoSpace when the
completion ends with any of @=/:., as fish won't add a space
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
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>
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to #1257.
Fixes#1257
Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Make sure to filter the returned completions before we check if
there are valid completions left.
Fixes#1362
Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Some programs may output extra empty lines after the directive.
Those lines must be ignored for fish shell completion to work.
zsh and bash are not impacted.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
Make it easier for programs to provide shell completion by creating
the 'completion' command automatically.
If a 'completion' command is already provided by the program, Cobra
will use that one instead.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
In accordance with our adopted best practices, the main branch and the
next major release of Cobra will deprecate older and un-maintained
versions of Golang.
fix#1322
* Add some guiding principals to the project.
Establish an understanding between user and maintainer.
Set a goal for releases, security fixes and bug patches.
* fix grammatical errors
The current powershell completion is not very capable.
Let's port it to the go custom completion logic to have a
unified experience accross all shells.
Powershell supports three different completion modes
- TabCompleteNext (default windows style - on each key press the next option is displayed)
- Complete (works like bash)
- MenuComplete (works like zsh)
You set the mode with `Set-PSReadLineKeyHandler -Key Tab -Function <mode>`
To keep it backwards compatible `GenPowerShellCompletion` will not display descriptions.
Use `GenPowerShellCompletionWithDesc` instead. Descriptions will only be displayed with
`MenuComplete` or `Complete`.
Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
* Add ORY Hydra & Kratos to projects_using_cobra.md
* fix: alphabetical order
my bad!
* fix: ORY to Ory
I think now it should be good,
sorry for the confusion!
yaml.v2 contains a breaking change from
https://github.com/go-yaml/yaml/pull/571
yaml.v2 2.2.8 does not have that change and also addresses the CVE that
was behind the move to yaml.v2 2.3.0. This commit reverts to using
yaml.v2 2.2.8
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
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.
* Fix stderr printing functions
Follow-up of #822
* Errors go to stderr as per POSIX
* use PrintErrf() instead of extra call to Sprintf()
* Error messages should always be printed to os.Stderr.
* add test case for Print* redirection
Thanks: @bukowa for the patch.