Commit graph

219 commits

Author SHA1 Message Date
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
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
namusyaka 7df62f7668
fix typos (#1274) 2020-12-04 12:34:11 -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
Michael Muré 8cfa4b4acf
Add documentation for Use (#1188) 2020-08-18 14:14:09 -06:00
Marc Khouzam 04318720db
Add completion for help command (#1136)
* Don't exclude 'help' from bash completions

Fixes #1000.

* Provide completion for the help command

1- Show 'help' as a possible completion
2- Provide completions for the help command itself

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Co-authored-by: Zaven Muradyan <voithos@google.com>
2020-06-16 14:49:26 -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
Marc Khouzam b84ef40338
Rename BashCompDirectives to ShellCompDirectives (#1082)
Since the completion directives will be used for all shells, and that
these names will be consumed by users, this is a more appropriate name.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2020-04-06 11:28:44 -06:00
Marc Khouzam b80aeb17fc
Add support for custom completions in Go (#1035)
This commit allows programs using Cobra to code their custom completions
in Go instead of Bash.

The new ValidArgsFunction field is added for commands, similarly to
ValidArgs.  For flags, the new function
Command.RegisterFlagCompletionFunc() is added.

When either of the above functions is used, the bash completion script
will call the new hidden command '__complete', passing it all
command-line arguments. The '__complete' command will call
the function specified by Command.ValidArgsFunction or by
Command.RegisterFlagCompletionFunc to obtain completions from the
program itself.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2020-04-03 13:43:43 -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
Daniel Esponda 3c2624538b
Correct documentation for InOrStdin (#929)
Documentation on function is incorrect
2020-02-20 11:42:29 -07:00
Alexandr Burdiyan 0da0687426
Add support for context.Context 2020-02-20 07:29:50 +01:00
Marc Khouzam 0d9d2d46f3 Revert change so help is printed on stdout again (#1004)
Fixes #1002
For backwards compatibility reasons, and to follow the need of
https://github.com/kubernetes/kubernetes/pull/26077#issuecomment-230818900
the help message should be printed on stdout.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2019-12-23 13:38:24 -05:00
Bruce Downs 993cc5372a Adjustments per PR review feedback from @bogem 2019-08-02 01:25:21 +05: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 b635726081 considering stderr in UsageString 2019-06-07 10:48:23 -04:00
Juan Leni cb27ce11fb Deprecate and maintain backwards compatibility 2019-06-07 10:48:23 -04:00
Juan Leni 618bc00f80 Allow for explicit output to err/stderr 2019-06-07 10:48:23 -04:00
Juan Leni 0ea93dd60d Fixed linter issues 2019-06-07 10:48:23 -04:00
Juan Leni 4a716d101b Extending redirection to stdout, stderr, stdin 2019-06-07 10:48:23 -04:00
umarcor 5755ecf102 [TrivialPatches] Typos in README.md, fix and update CI, update projects list... (#840)
* update Example in README.md (#769)

* specify the color as the required arg (#777)

* command: fix typo in docstring of InheritedFlags (#779)

* add istio to the list of projects built with Cobra (#786)

* remove redundant 'else' (#806)

* add mattermost-server as a project built with Cobra (#824)

* update README.md (#826)

Fix the comment: consistent with others

* add uber/prototool as a project built with Cobra (#831)

* fix(ci): use go vet, update to Go 1.12, update shellcheck to v0.4.6 (#832)

* add go.mod and go.sum (#833)

* chore(travis): move 'diff' job to separate stage in Travis (#839)

* chore(travis): use language configuration list instead of explicit entries in matrix.include (#839)

* chore(travis): update shellcheck-docker to v0.6.0 (#839)

* update(README.md): separate projects by commas, instead of using a list

* chore: update viper to v1.3.2 and go-md2man to v1.0.10

* fix: convert CRLF to LF when comparing files

* use kyoh86/richgo to provide colored test outputs
2019-03-20 17:21:26 -04:00
Rajat Jindal 4dab30cb33 Add support for ignoring parse errors (#662) 2018-03-31 08:36:20 -04:00
Di Xu c439c4fa09 Terminate the stripping of flags when -- is found (#649) 2018-03-19 11:20:04 +05: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
Shamus Taylor 3a7eb146ba fix grammatical error in command.go (#626) 2018-02-01 22:04:56 -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
Daniel Nephin 34ceca591b Disable flags in UseLine (#552)
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-11-19 08:58:54 +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
Pavol Loffay f634327172 Fix typo (#573) 2017-11-08 11:47:54 +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
Albert Nigmatzianov 40f18800b2 Delete name field from Command (#547)
Discussion: https://github.com/spf13/cobra/pull/422#discussion_r143918343
2017-10-11 10:15:37 -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
Diego Becciolini 0dacccfbaa Improve consistency of flags when using SetGlobalNormalizationFunc (#522)
Fix #521
2017-10-02 12:00:25 +02:00
Albert Nigmatzianov 1723331773 Update documentation 2017-08-23 12:32:09 +05:00
Daniel Nephin f20b4e9c32 Refactor TakesArgs to use an interface for arg validation.
Fix some typos in README and comments.
Move arg validation to after flag validation so that the help flag is run first.
Pass the same args to ValidateArgs as the Run methods receive.
Update README.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-07-23 09:57:27 +02:00
Eric Paris d89c499964 Allow commands to explicitly state if they do, or do not take arbitrary arguments
Check that arguments are in ValidArgs

If a command defined cmd.ValidArgs check that the argument is actually
in ValidArgs and fail if it is not.
2017-07-23 09:57:27 +02:00
Albert Nigmatzianov d994347eda Delete tab in the long description of help 2017-07-16 12:48:02 +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 e606913c4e Fix typos 2017-05-28 12:56:30 +02:00
Albert Nigmatzianov 52ae6a1d02 Make initHelpCmd public (#436)
Useful for solving #424
2017-05-20 19:23:33 +02:00
Albert Nigmatzianov 51b7cf57e1 Fix tests so they give correct args (#445)
* Fix tests so they give correct args

Shell already deletes all quotes and unite args under quotes, so we
don't need to test it.

* Simplify stripFlags

* Fix 'unused' and 'gosimple' complaints
2017-05-14 08:51:04 -04:00
Albert Nigmatzianov 7d22e77cce Fix incorrect merge of pflag.CommandLine to cmd.Flags()
Fix #443
2017-05-12 20:22:26 +02:00