This commit changes the flag groups feature logic. New implementation is more clean, readable and extendable (hope it won't be just my opinion).
The following changes have been made:
1. Main change:
Flags annotating by "cobra_annotation_required_if_others_set" and "cobra_annotation_mutually_exclusive" annotations was removed as well as all related and hard-to-understand "hacks" to combine flags back into groups on validation process.
Instead, `flagGroups` field was added to the `Command` struct. `flagGroups` field is a list of (new) structs `flagGroup`, which represents the "relationships" between flags within the command.
2. "Required together" and "mutually exclusive" groups logic was updated by implementing `requiredTogetherFlagGroup` and `mutuallyExclusiveFlagGroup` `flagGroup`s.
3. `enforceFlagGroupsForCompletion` `Command`'s method was renamed to `adjustByFlagGroupsForCompletions`.
4. Groups failed validation error messages were changed:
- `"if any flags in the group [...] are set they must all be set; missing [...]"` to `"flags [...] must be set together, but [...] were not set"`
- `"if any flags in the group [...] are set none of the others can be; [...] were all set"` to `"exactly one of the flags [...] can be set, but [...] were set"`
5. Not found flag on group marking error messages were updated from "Failed to find flag %q and mark it as being required in a flag group" and "Failed to find flag %q and mark it as being in a mutually exclusive flag group" to "flag %q is not defined"
6. `TestValidateFlagGroups` test was updated in `flag_groups_test.go`.
- `getCmd` function was updated and test flag names were changed to improve readability
- 2 testcases (`Validation of required groups occurs on groups in sorted order` and `Validation of exclusive groups occurs on groups in sorted order`) were removed, because groups validation now occur in the same order those groups were registered
- other 16 testcases are preserved with updated descriptions, error messages
The completions generation tests that contain flag groups related testcases and updated flag groups tests, as well as all other tests, have been passed.
API was not changed: `MarkFlagsRequiredTogether` and `MarkFlagsMutuallyExclusive` functions have the same signatures.
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>
When a user has an alias in powershell, she will need to register that
alias for completion. To make that possible, we store the completion
logic into a scriptblock variable which can easily be accessed by the
user to register aliases.
For example, if the user defines an alias for `helm`:
PS> sal h helm
she will need to register the alias like so:
PS> Register-ArgumentCompleter -CommandName 'h' -ScriptBlock $__helmCompleterBlock
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This method is the OnInitialize counterpart. Like OnInitialize which allows
loading the configuration before each command is executed, OnFinalize allows
saving the configuration after each command has been executed.
Add a global `EnableCaseInsensitive` variable to allow
case-insensitive command names.
The variable supports commands names and aliases globally.
Resolves#1382
The format "go install github.com/kyoh86/richgo@latest" will work with
go 1.16 and higher. It will not work with go 1.15. However, since
installing "richgo" is only required for people who want to run the go
tests for the Cobra project itself, I feel it is ok to require go 1.16
or higher in this case.
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
* ci: test on Golang 1.19
* ci: run golangci-lint on golang 1.19
* Adds `check-latest` for setup-go action v3
* ci/golangci-lint: use latest version available in setup-go's manifest
Signed-off-by: John McBride <jpmmcbride@gmail.com>
Signed-off-by: umarcor <unai.martinezcorral@ehu.eus>
Co-authored-by: John McBride <jpmmcbride@gmail.com>
This fixes a bug where a child flag that shadows (has the same
name as) a parent persistent flag would not be shown in the
child command's help output and the parent flag would be shown
instead under the global flags section.
This change makes the help output consistent with the
observed behavior during execution, where the child flag is
the one that is actually used.
```shell
$ set -u
$ foo=()
$ echo ${#foo}
bash: foo: unbound variable
echo ${#foo[*]}
0
```
The above shows that an empty array needs the suffix `[*]` when checking its length, or else it is considered unbound.
The `see_also` section for child commands would include only the name of
the commands. This adds the whole path, similar to how it's done for the
other documentation formats.
Reaching out on behalf of KubeVirt, an add-on for Kubernetes, enabling
users to run Virtual Machines on Kubernetes pods.
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
Since go 1.13 you can wrap errors. This make it no longer possible to
compare with `==`, instead you have to compare with `errors.Is()`.
I noticed this problem because -h was no longer working after I stared
wrapping the errors in my custom FlagErrorFunc function.
Note that this is only a problem when a custom help flag is defined.
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Fixes#1734
Tab characters that introduce completion descriptions weren't properly
being handled with bash v3. This change fixes that.
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Previously the generated zsh completion script started with the line
#compdef _<command> <command>
where <command> is the command that the zsh completion script is
generated for.
This enabled completions for both <command> and _<command>, but
_<command> is the completion function itself and should not be
completed. Furthermore, attempting to autocomplete _<command> (e.g.
typing "_<command><Space><Tab>" in a zsh shell) causes zsh to hang.
This commit fixes the #compdef line to only complete <command>, not
_<command>.
Co-authored-by: Arvid Norlander <VorpalBlade@users.noreply.github.com>
Not that it'd really matter that much performancewise given the level we
are at for this case, but this change makes the short circuit roughly
twice as fast on my box as it was for the 1000 rounds done in
marckhouzam/cobra-completion-testing.
Perhaps more importantly, this makes the code arguably slightly cleaner.