Commit Graph

21 Commits

Author SHA1 Message Date
Ville Skyttä
e63925d321
Add env variable to suppress completion descriptions on create (#1938)
COBRA_COMPLETION_DESCRIPTIONS=0 
or 
<PROGRAM>_COMPLETION_DESCRIPTIONS=0
can now be used to disable shell completion descriptions.
2023-12-17 13:50:59 -05:00
Marc Khouzam
a0a6ae020b
Improve API to get flag completion function (#2063)
The new API is simpler and matches the `c.RegisterFlagCompletionFunc()`
API.  By removing the global function `GetFlagCompletion()` we are more
future proof if we ever move from a global map of flag completion
functions to something associated with the command.

The commit also makes this API work with persistent flags by using
`c.Flag(flagName)` instead of `c.Flags().Lookup(flagName)`.

The commit also adds unit tests.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2023-11-02 11:23:08 -04:00
Marc Khouzam
00b68a1c26
Add tests for flag completion registration (#2053)
Different problems have been reported about flag completion registration.
These two tests are the cases that were not being verified but had been
mentioned as problematic.

Ref:
- https://github.com/spf13/cobra/issues/1320
- https://github.com/spf13/cobra/pull/1438#issuecomment-872928669

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2023-10-28 16:11:59 -04:00
Marc Khouzam
b711e8760b
Don't complete --help flag when flag parsing disabled (#2061)
Fixes #2060

When a command sets `DisableFlagParsing = true` it requests the
responsibility of doing all the flag parsing. Therefore even the
`--help/-f/--version/-v` flags should not be automatically completed
by Cobra in such a case.

Without this change the `--help/-h/--version/-v` flags can end up being
completed twice for plugins: one time from cobra and one time from the
plugin (which has set `DisableFlagParsing = true`).

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2023-10-28 16:10:06 -04:00
Martijn Evers
c81c46a015
Add 'one required flag' group (#1952) 2023-07-16 12:38:22 -04:00
Unai Martinez-Corral
9e6b58afc7
update copyright year (#1927) 2023-03-05 21:28:31 -05:00
Dominik Roos
b4f979ae35
completions: do not detect arguments with dash as 2nd char as flag (#1817)
Fixes #1816

Previously, arguments with a dash as the second character (e.g., 1-ff00:0:1)
were detected as a flag by mistake. This resulted in auto completion misbehaving
if such an argument was last in the argument list during invocation.
2023-01-02 19:58:36 -05:00
Marc Khouzam
212ea40783
Include --help and --version flag in completion (#1813)
Fixes #1786

The --help, -h, --version and -v flags are normally added when the
`execute()` function is called on a command.  When doing completion
we don't call `execute()` so we need to add these flags explicitly to
the command being completed.

Also, we disable all further completions if the 'help' or 'version'
flags are present on the command-line.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2022-10-04 16:41:30 -06:00
Unai Martinez-Corral
6d978a911e
add missing license headers (#1809) 2022-09-16 07:55:56 -04:00
Marc Khouzam
5f2ec3c897
Update shell completion to respect flag groups (#1659)
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Co-authored-by: Marc Khouzam <marc.khouzam@montreal.ca>
2022-06-20 20:04:28 -06:00
Simon Ser
5d066b77b5
Introduce FixedCompletions (#1574)
Example usage:

    choices := []string{"choice1", "choice2", "choice3"}
    cmd.RegisterFlagCompletionFunc(cobra.FixedCompletions(choices, ShellCompDirectiveNoFileComp))
2022-03-17 21:37:54 -04:00
Marc Khouzam
9054739e08
Remove __complete cmd for program without subcmds (#1563)
Fixes #1562

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 (#1541)
Fixes #1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2021-12-07 16:02:02 -07:00
Frank Chiarulli Jr
c3573e220c
Completion: Capitalize short desc, and remove extra space from long (#1455) 2021-12-07 15:52:50 -07:00
Marc Khouzam
d2c0cb310d
DisableFlagParsing must disable flag completion (#1161)
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 (#1438)
* 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 #1437
Fixes #1320

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
Paul Holzinger
5d46ac904d
custom comp: do not complete flags after args when interspersed is false (#1308)
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
Marc Khouzam
b36196066e
Bash completion V2 with completion descriptions (#1146)
* 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
Paul Holzinger
2d94892a8b
Custom completion handle multiple shorhand flags together (#1258)
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>
2021-05-03 10:42:00 -06:00
Lukas Malkmus
6d00909120
Pass context to completion (#1265) 2021-05-03 10:33:57 -06:00
Marc Khouzam
b312f0a8ef
Create 'completion' command automatically (#1192)
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