Commit Graph

19 Commits

Author SHA1 Message Date
Simon Ser 5d066b77b5
Introduce FixedCompletions ()
Example usage:

    choices := []string{"choice1", "choice2", "choice3"}
    cmd.RegisterFlagCompletionFunc(cobra.FixedCompletions(choices, ShellCompDirectiveNoFileComp))
2022-03-17 21:37:54 -04:00
Ondrej Sika 8cc7be2119
feat: Add how to load completions in your current zsh session () 2022-03-17 17:55:16 -04:00
cui fliter 81d27c8f8d
fix some typos ()
* fix some typos

Signed-off-by: cuishuang <imcusg@gmail.com>
2022-03-14 07:05:53 -04:00
Marc Khouzam 9054739e08
Remove __complete cmd for program without subcmds ()
Fixes 

Programs that don't have sub-commands can accept any number of args.
However, when doing shell completion for such programs, within the
__complete code this very __complete command makes it that the program
suddenly has a sub-command, and the call to Find() -> legacyArgs() will
then return an error if there are more than one argument on the
command-line being completed.

To avoid this, we first remove the __complete command in such a case so
as to get back to having no sub-commands.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2021-12-14 11:22:22 -07:00
Marc Khouzam 6f84ef4875
Provide option to hide default 'completion' cmd ()
Fixes 

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2021-12-07 16:02:02 -07:00
meyermarcel ee75a2b1ed
Remove trailing spaces from bash completion command description () 2021-12-07 16:01:22 -07:00
Frank Chiarulli Jr c3573e220c
Completion: Capitalize short desc, and remove extra space from long () 2021-12-07 15:52:50 -07:00
Carlos Alexandro Becker dd40ab071f
feat: improve completions help formatting ()
Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
2021-12-07 15:51:48 -07:00
Marc Khouzam d2c0cb310d
DisableFlagParsing must disable flag completion ()
When a command has set DisableFlagParsing=true, it means Cobra should
not be handling flags for that command but should let the command
handle them itself.  In fact, Cobra normally won't have been told at all
about flags for this command.

Not knowing about the flags of the command also implies that Cobra
cannot perform flag completion as it does not have the necessary info.

This commit still tries to do flag name completion, but when
DisableFlagParsing==true, it continues on so that ValidArgsFunction will
be called; this allows the program to handle completion for its own
flags when DisableFlagParsing==true.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2021-11-01 13:01:33 -06:00
Paul Holzinger de187e874d
Fix flag completion ()
* 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  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 
Fixes 

Signed-off-by: Paul Holzinger <pholzing@redhat.com>

* Fix trailing whitespaces in fish comp scripts

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2021-07-02 09:25:47 -06:00
Kyle Lemons 5738d6b72d
Add install instructions for zsh on Mac OS ()
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.
2021-07-01 09:49:12 -06:00
Paul Holzinger 5d46ac904d
custom comp: do not complete flags after args when interspersed is false ()
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>
2021-07-01 09:47:10 -06:00
silenceshell 3c8a19ecd3
fix RegisterFlagCompletionFunc concurrent map writes error ()
* fix-RegisterFlagCompletionFunc-concurrent
* set to root command
* move to non-public fields
2021-06-30 15:49:30 -06:00
Marc Khouzam b36196066e
Bash completion V2 with completion descriptions ()
* 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>
2021-06-30 15:24:58 -06:00
tamo 4590150168
Correcting misspelled words ()
* Correcting Misspelled Words
* grammar fixes
2021-05-10 17:19:33 -06:00
Marc Khouzam 95d23d24ff
Fix zsh for DirectiveNoSpace and DirectiveNoFileComp ()
Fixes 

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 

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
Paul Holzinger 2d94892a8b
Custom completion handle multiple shorhand flags together ()
Flag definitions like `-asd` are not handled correctly by
the custom completion logic. They should be treated as
multiple flags. For details refer to .

Fixes 

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2021-05-03 10:42:00 -06:00
Lukas Malkmus 6d00909120
Pass context to completion () 2021-05-03 10:33:57 -06:00
Marc Khouzam b312f0a8ef
Create 'completion' command automatically ()
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>
2021-02-15 10:47:01 -07:00