Commit Graph

66 Commits

Author SHA1 Message Date
Michael Vogt a75c389fec args: add new `UnknownCommandError` type
This commit makes unknown commands a proper error type so that
the caller can check for this specific error type without having
to do string comparisons.

This is useful in the context of https://github.com/spf13/cobra/issues/823

Signed-off-by: Michael Vogt <mvogt@redhat.com>
Signed-off-by: Michael Vogt <michael.vogt@gmail.com>
2024-08-19 08:39:35 +02:00
Ville Skyttä 6b5f577ebc
More linting (#2099)
* Address gocritic findings, enable it

* Enable gosimple, no new findings to address
2024-04-01 08:42:08 -04:00
Pedro Mota bd914e58d6
fix: remove deprecated io/ioutils package (#2120)
ioutils.ReadAll is deprecated since Go 1.16. This commit replaces it with
io.ReadAll. See https://pkg.go.dev/io/ioutil\#ReadAll for reference

Issue #2119
2024-03-12 06:42:46 -04:00
Nir Soffer a73b9c391a Fix help text for runnable plugin command
When creating a plugin without sub commands, the help text included the
command name (kubectl-plugin) instead of the display name (kubectl plugin):

    Usage:
      kubectl-plugin [flags]

The issue is that the usage line for this case does not use the
command path but the raw `Use` string, and this case was not tested.

Add a test for this case and fix UsageLine() to replace the command name
with the display name.

Tested using https://github.com/nirs/kubernetes/tree/sample-cli-plugin-help
2023-12-17 19:58:57 -05:00
Nir Soffer df547f5fc6 Fix help text for plugins
When using `CommandDisplayNameAnnotation` we want to use it instead of
the command name in `--help` message or in the default help command.

With current code we get the wrong text in the --help usage text:

    Flags:
      -h, --help   help for kubectl-plugin

And in the long description of the default help command:

    $ kubectl cobraplugin help -h
    Help provides help for any command in the application.
    Simply type kubectl-plugin help [path to command] for full details.

The issue was hidden since the test checked only the Usage line.

Fixed by extracting a displayName() function and use it when creating
FlagSet and when formatting the default help flag usage and the help
command long description.

Enhance the TestPlugin to check all the lines including the command
name.
2023-12-17 19:58:57 -05:00
Nir Soffer 890302a35f
Support usage as plugin for tools like kubectl (#2018)
In this case the executable is `kubectl-plugin`, but we run it as:

    kubectl plugin

And the help text should reflect the actual usage of the command.

To create a plugin, add the cobra.CommandDisplayNameAnnotation:

    rootCmd := &cobra.Command{
        Use: "plugin",
        Annotations: map[string]string{
            cobra.CommandDisplayNameAnnotation: "kubectl plugin",
        }
    }

Internally this change modifies CommandPath() for the root command to
return the command display name instead of the command name. This is
used for error messages, help text generation, and completions.

CommandPath() is expected to have spaces and code using it already
handle spaces (e.g replacing with _), so hopefully this does not break
anything.

Fixes: #2017

Signed-off-by: Nir Soffer <nsoffer@redhat.com>
2023-11-02 08:15:26 -04:00
vkhoroz 4cafa37bc4
Allow running persistent run hooks of all parents (#2044)
Currently, only one of the persistent pre-runs and post-runs is executed.
It is always the first one found in the parents chain, starting at this command.
Expected behavior is to execute all parents' persistent pre-runs and post-runs.

Dependent projects implemented various workarounds for this:
- manually building persistent hook chains (in every hook).
- applying some kind of monkey-patching on top of Cobra.

This change eliminates the necessity for such workarounds
by allowing to set a global variable EnableTraverseRunHooks.

Tickets:
- https://github.com/spf13/cobra/issues/216
- https://github.com/spf13/cobra/issues/252

Signed-off-by: Volodymyr Khoroz <volodymyr.khoroz@foundries.io>
2023-10-21 20:36:12 -04:00
Souma 0c72800b8d
Customizable error message prefix (#2023) 2023-09-08 13:29:06 -04:00
Jun Nishimura fd865a44e3
minor corrections to unit tests (#2003) 2023-07-23 07:31:55 -04:00
Unai Martinez-Corral 9e6b58afc7
update copyright year (#1927) 2023-03-05 21:28:31 -05:00
Brian Pursley ad6db7f8f6
Create unit test illustrating unknown flag bug (#1854)
Created a unit test that tests the unknown flag
error message when the unknown flag is located
in different arg positions.
2022-11-14 21:46:57 -05:00
Brian Pursley 6b0bd3076c
fix: don't remove flag value that matches subcommand name (#1781)
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.
2022-11-07 23:12:02 -05:00
Marc Khouzam 10cf7be997
Check for group presence after full initialization (#1839)
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>
2022-10-24 11:11:57 -04:00
aawsome 2169adb574
Add groups for commands in help (#1003)
* 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>
2022-10-10 14:59:11 -06:00
Francis Nickels III 7039e1fa21
Add '--version' flag to Help output (#1707) 2022-09-30 14:26:05 -04:00
Unai Martinez-Corral 6d978a911e
add missing license headers (#1809) 2022-09-16 07:55:56 -04:00
Yuval Goldberg d689184a42
Support for case-insensitive command names (#1802)
Add a global `EnableCaseInsensitive` variable to allow
case-insensitive command names.

The variable supports commands names and aliases globally.

Resolves #1382
2022-09-11 08:25:22 -04:00
Brian Pursley 22b617914c
fix: show flags that shadow parent persistent flag in child help (#1776)
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.
2022-08-28 12:46:39 -04:00
Paul Holzinger b9ca5949e2
use errors.Is() to check for errors (#1730)
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>
2022-06-20 20:02:33 -06:00
Joshua Carpeggiani f848943afd
Add Command.SetContext (#1551)
Increases flexibility in how Contexts can be used with Cobra.
2022-03-18 06:01:58 -04:00
Stefan Weil 1854bb5c96
Fix some typos (mostly found by codespell) (#1514)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2021-12-07 16:06:52 -07:00
Lukas Malkmus 6d00909120
Pass context to completion (#1265) 2021-05-03 10:33:57 -06:00
Unai Martinez-Corral 652c755d37
Use golangci-lint (#1044)
Use golangci-lint. Repair warnings and errors resulting from linting.
2021-02-07 17:08:50 -07:00
Alessio Treglia 40d34bca1b
Fix stderr printing functions (#894)
* 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.
2020-10-01 09:28:00 -06:00
Marc Khouzam 5155946348
Ignore required flags when DisableFlagParsing (#1095)
When a command request to DisableFlagParsing, it should not fail due
to a missing required flag.  In fact, such a check will always fail
since flags weren't parsed!

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2020-05-07 19:18:16 -06:00
John McBride 7fead4bf3b
Remove/replace SetOutput on Command - deprecated (#1078)
Replace SetOutput on Command - deprecated
2020-04-06 11:36:04 -06:00
Kanji Yomoda f62883520e
Replace deprecated SetOutput func with SetOut and SetErr in test (#1053) 2020-04-01 10:25:22 -06:00
Joshua Harshman 6607e6b860
Partial Revert of #922 (#1068)
Issue Reference: https://github.com/spf13/cobra/issues/1056

https://github.com/spf13/cobra/pull/922 introduced a new error
type that emitted when a command was not runnable. This caused
all commands w/o a run function set to error w/ that message and a status code of 1.

This change reverts the addition of that new error. Similar
functionality can be accomplished by leveraging RunE.
2020-03-27 14:38:32 -06:00
Dave Henderson 95f2f73ed9
Add short version flag -v when not otherwise set (#996)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
2020-02-28 11:13:40 -07:00
Alexandr Burdiyan 0da0687426
Add support for context.Context 2020-02-20 07:29:50 +01:00
Bruce Downs 51f06c7dd1 Correct all complaints from golint
* i.e.
* go get golang.org/x/lint/golint
* go list ./... | xargs golint
2019-08-02 01:25:21 +05:00
Bruce Downs 9334a46bd6 Return an error in the case of unrunnable subcommand
* credit to @chriswhelix for initial commit
2019-08-02 01:25:21 +05:00
Juan Leni f2b07da1e2 fixing linter issues 2019-06-07 10:48:23 -04:00
Juan Leni b635726081 considering stderr in UsageString 2019-06-07 10:48:23 -04:00
Alessio Treglia e35034f0da Add tests 2019-06-07 10:48:23 -04:00
Hyang-Ah Hana Kim a114f312e0 fix test build breakage with go1.11 (#712)
As discussed in golang/go#26109, vet typecheck is enabled
in go1.11 and the command_test.go can't be compiled any
more with go1.11 due to the unread variables in the
command_test.go.

Instead of removing the unused variables, this CL reads the
variables and compares the values against the current
behavior so when the related issue is fixed, this test can
be updated accordingly.
2018-06-29 11:25:35 -04:00
Rajat Jindal 4dab30cb33 Add support for ignoring parse errors (#662) 2018-03-31 08:36:20 -04:00
Tim Peoples eb58983359 Add `CalledAs` method to cobra.Command (w/ tests) (#567)
* Add `CalledAs` method to Command (w/ tests)

The `CalledAs` method returns the name of the command or alias that
invoked the command -- as long as the command was actually invoked.
Otherwise, it returns the empty string.

The opens up possibilies for commands to behave differently based on
which alias invoked the command (in the same vein as Linux programs
which adjust their behavior based on the value of argv[0]).

* Fixed formatting
2018-02-04 08:58:53 -08:00
Nick Miyake ccaecb155a Ensure that '--version' flag works properly for root command (#595)
Make it so that, in the case that the root command is not runnable
but has subcommands, specifying a '--version' flag will still
run the "version" behavior.
2017-12-07 08:49:35 +01:00
Nick Miyake b1ec2ce1ad Add support for --version flag (#584) 2017-12-01 22:37:16 +01:00
Nick Miyake 19e54c4a2b Update error message for missing required flags (#580)
Make it so that first letter is not capitalized and rephrase
to remove "have/has".
2017-11-19 10:22:51 +01:00
Albert Nigmatzianov d6a430541c Edit ResetFlags and ResetCommands descriptions 2017-11-09 07:56:43 +01:00
Albert Nigmatzianov 65c8acb228 Improve tests 2017-11-09 07:56:43 +01:00
Thomas Cyron 7b2c5ac9fc Create new buffer if not present yet (#549)
Fixes a nil dereference when TraverseChildren is used
with multiple subcommands.
2017-10-12 20:25:33 +02:00
Di Xu 7cd9cc6d44 add test for c.Name() if c.Use gets changed (#548) 2017-10-12 12:50:22 -04:00
Di Xu 4d6af280c7 enforce required flags (#502) 2017-10-09 23:44:33 -04:00
Daniel Nephin 83b1f03962 Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)
Fix #277 
Fix #467
2017-10-07 23:29:11 +02:00
Albert Nigmatzianov 8c6fa02d22 Fix InitDefaultHelpCmd when custom help command is set 2017-06-29 12:52:34 +02:00
Albert Nigmatzianov 99b5d838ca Show messages if deprecated flags are used
Fix #463
2017-06-19 23:05:29 +02:00
Albert Nigmatzianov 7d22e77cce Fix incorrect merge of pflag.CommandLine to cmd.Flags()
Fix #443
2017-05-12 20:22:26 +02:00