When the command searches args to find the arg matching a
particular subcommand name, it needs to ignore flag values,
as it is possible that the value for a flag might match
the name of the sub command.
This change improves argsMinusFirstX() to ignore flag values
when it searches for the X to exclude from the result.
Fixes#1831
By moving the check for help group existence to "ExecuteC()" we no
longer need groups to be added before AddCommand() is called. This
provides more flexibility to developers and works better with the use
of "init()" for command creation.
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
* use arithmetic evaluation in numeric context
* remove unnecessary $ from array index variables
* [[ ]] over [ ], == over =, remove unnecessary quoting
* use ${foo-} rather than ${foo:-} in emptiness check
The result of the expansion is null no matter if the variable is unset
or null in both cases; the former form is arguably easier on the eye.
* remove unnecessary trailing linefeed removal
No longer needed as of f464d6c82e, saves
a subshell.
* use herestring in activehelp extraction
Herestrings read cleaner than process substitutions, and work in posix
mode (but we do and will have some process substitutions so this doesn't
matter much). Both approaches may end up using temporary files.
* Add tests for grouping commands
* Adds Additional Command section in help
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
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>