Commit graph

59 commits

Author SHA1 Message Date
maxlandon
4d3a6e1d56
Merge master, and adapt flag completion getters 2023-09-30 20:43:04 +02: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
Albert Nigmatzianov
fe69f2e3a3 Make initHelpFlag public
Used for solving #424
2017-05-07 00:45:39 +02:00
Albert Nigmatzianov
d20925b932 cmd: Small correctives 2017-05-05 10:35:27 +02:00
Albert Nigmatzianov
ea3c5beeff Add TestHelpFlagInHelp 2017-04-26 21:24:14 +02:00
Albert Nigmatzianov
f58a8d6bd3 Add TestSortedFlags 2017-04-20 15:38:50 +02:00
Tamir Duberstein
7aeaa2cce6 Avoid storing pointer to nil (#411)
* Fix shellcheck

Before this change:

	In - line 204:
	    declare -F $next_command >/dev/null && $next_command
	               ^-- SC2086: Double quote to prevent globbing and word splitting.

	--- FAIL: TestBashCompletions (0.34s)
		bash_completions_test.go:138: shellcheck failed: exit status 1

* Avoid storing pointer to nil

Before this change, the new test fails with:

	--- FAIL: TestSetOutput (0.00s)
		command_test.go:198: expected setting output to nil to revert back to stdout, got <nil>
2017-04-02 10:14:34 -04:00
Ben Gadbois
6e91dded25 Make gofmt -s pass (#355) 2016-10-25 18:28:26 -07:00
Eric Paris
ec2fe78599 Merge pull request #286 from dnephin/merge_flags_before_adding_help
Merge persistent flags before checking for a help flag
2016-10-10 11:14:18 -04:00