* Fix flag completion
The flag completion functions should not be stored in the root cmd.
There is no requirement that the root cmd should be the same when
`RegisterFlagCompletionFunc` was called. Storing the flags there does
not work when you add the the flags to your cmd struct before you add the
cmd to the parent/root cmd. The flags can no longer be found in the rigth
place when the completion command is called and thus the flag completion
does not work.
Also #1423 claims that this would be thread safe but we still have a map
which will fail when accessed concurrently. To truly fix this issue use a
RWMutex.
Fixes#1437Fixes#1320
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
* Fix trailing whitespaces in fish comp scripts
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
* Bash completion v2
This v2 version of bash completion is based on Go completions.
It also supports descriptions like the other shells.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Only consider matching completions for formatting
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Use bash compV2 for the default completion command
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Update comments that still referred to bash completion
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
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>
* 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.
* 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>
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>
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>
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>
Issue Reference: https://github.com/spf13/cobra/issues/1056https://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.
* 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
* 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
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.
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>