go.mod: spf13/cobra v1.0.0
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
f9c1b86feb
commit
79ead619be
43 changed files with 3224 additions and 455 deletions
151
vendor/github.com/spf13/cobra/command.go
generated
vendored
151
vendor/github.com/spf13/cobra/command.go
generated
vendored
|
@ -17,6 +17,7 @@ package cobra
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
@ -56,6 +57,10 @@ type Command struct {
|
|||
|
||||
// ValidArgs is list of all valid non-flag arguments that are accepted in bash completions
|
||||
ValidArgs []string
|
||||
// ValidArgsFunction is an optional function that provides valid non-flag arguments for bash completion.
|
||||
// It is a dynamic version of using ValidArgs.
|
||||
// Only one of ValidArgs and ValidArgsFunction can be used for a command.
|
||||
ValidArgsFunction func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)
|
||||
|
||||
// Expected arguments
|
||||
Args PositionalArgs
|
||||
|
@ -80,7 +85,8 @@ type Command struct {
|
|||
|
||||
// Version defines the version for this command. If this value is non-empty and the command does not
|
||||
// define a "version" flag, a "version" boolean flag will be added to the command and, if specified,
|
||||
// will print content of the "Version" variable.
|
||||
// will print content of the "Version" variable. A shorthand "v" flag will also be added if the
|
||||
// command does not define one.
|
||||
Version string
|
||||
|
||||
// The *Run functions are executed in the following order:
|
||||
|
@ -140,9 +146,11 @@ type Command struct {
|
|||
// TraverseChildren parses flags on all parents before executing child command.
|
||||
TraverseChildren bool
|
||||
|
||||
//FParseErrWhitelist flag parse errors to be ignored
|
||||
// FParseErrWhitelist flag parse errors to be ignored
|
||||
FParseErrWhitelist FParseErrWhitelist
|
||||
|
||||
ctx context.Context
|
||||
|
||||
// commands is the list of commands supported by this program.
|
||||
commands []*Command
|
||||
// parent is a parent command for this command.
|
||||
|
@ -177,8 +185,6 @@ type Command struct {
|
|||
// that we can use on every pflag set and children commands
|
||||
globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
|
||||
|
||||
// output is an output writer defined by user.
|
||||
output io.Writer
|
||||
// usageFunc is usage func defined by user.
|
||||
usageFunc func(*Command) error
|
||||
// usageTemplate is usage template defined by user.
|
||||
|
@ -195,6 +201,19 @@ type Command struct {
|
|||
helpCommand *Command
|
||||
// versionTemplate is the version template defined by user.
|
||||
versionTemplate string
|
||||
|
||||
// inReader is a reader defined by the user that replaces stdin
|
||||
inReader io.Reader
|
||||
// outWriter is a writer defined by the user that replaces stdout
|
||||
outWriter io.Writer
|
||||
// errWriter is a writer defined by the user that replaces stderr
|
||||
errWriter io.Writer
|
||||
}
|
||||
|
||||
// Context returns underlying command context. If command wasn't
|
||||
// executed with ExecuteContext Context returns Background context.
|
||||
func (c *Command) Context() context.Context {
|
||||
return c.ctx
|
||||
}
|
||||
|
||||
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
|
||||
|
@ -205,8 +224,28 @@ func (c *Command) SetArgs(a []string) {
|
|||
|
||||
// SetOutput sets the destination for usage and error messages.
|
||||
// If output is nil, os.Stderr is used.
|
||||
// Deprecated: Use SetOut and/or SetErr instead
|
||||
func (c *Command) SetOutput(output io.Writer) {
|
||||
c.output = output
|
||||
c.outWriter = output
|
||||
c.errWriter = output
|
||||
}
|
||||
|
||||
// SetOut sets the destination for usage messages.
|
||||
// If newOut is nil, os.Stdout is used.
|
||||
func (c *Command) SetOut(newOut io.Writer) {
|
||||
c.outWriter = newOut
|
||||
}
|
||||
|
||||
// SetErr sets the destination for error messages.
|
||||
// If newErr is nil, os.Stderr is used.
|
||||
func (c *Command) SetErr(newErr io.Writer) {
|
||||
c.errWriter = newErr
|
||||
}
|
||||
|
||||
// SetIn sets the source for input data
|
||||
// If newIn is nil, os.Stdin is used.
|
||||
func (c *Command) SetIn(newIn io.Reader) {
|
||||
c.inReader = newIn
|
||||
}
|
||||
|
||||
// SetUsageFunc sets usage function. Usage can be defined by application.
|
||||
|
@ -267,9 +306,19 @@ func (c *Command) OutOrStderr() io.Writer {
|
|||
return c.getOut(os.Stderr)
|
||||
}
|
||||
|
||||
// ErrOrStderr returns output to stderr
|
||||
func (c *Command) ErrOrStderr() io.Writer {
|
||||
return c.getErr(os.Stderr)
|
||||
}
|
||||
|
||||
// InOrStdin returns input to stdin
|
||||
func (c *Command) InOrStdin() io.Reader {
|
||||
return c.getIn(os.Stdin)
|
||||
}
|
||||
|
||||
func (c *Command) getOut(def io.Writer) io.Writer {
|
||||
if c.output != nil {
|
||||
return c.output
|
||||
if c.outWriter != nil {
|
||||
return c.outWriter
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getOut(def)
|
||||
|
@ -277,6 +326,26 @@ func (c *Command) getOut(def io.Writer) io.Writer {
|
|||
return def
|
||||
}
|
||||
|
||||
func (c *Command) getErr(def io.Writer) io.Writer {
|
||||
if c.errWriter != nil {
|
||||
return c.errWriter
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getErr(def)
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func (c *Command) getIn(def io.Reader) io.Reader {
|
||||
if c.inReader != nil {
|
||||
return c.inReader
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getIn(def)
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// UsageFunc returns either the function set by SetUsageFunc for this command
|
||||
// or a parent, or it returns a default usage function.
|
||||
func (c *Command) UsageFunc() (f func(*Command) error) {
|
||||
|
@ -314,6 +383,8 @@ func (c *Command) HelpFunc() func(*Command, []string) {
|
|||
}
|
||||
return func(c *Command, a []string) {
|
||||
c.mergePersistentFlags()
|
||||
// The help should be sent to stdout
|
||||
// See https://github.com/spf13/cobra/issues/1002
|
||||
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
|
||||
if err != nil {
|
||||
c.Println(err)
|
||||
|
@ -329,13 +400,22 @@ func (c *Command) Help() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UsageString return usage string.
|
||||
// UsageString returns usage string.
|
||||
func (c *Command) UsageString() string {
|
||||
tmpOutput := c.output
|
||||
// Storing normal writers
|
||||
tmpOutput := c.outWriter
|
||||
tmpErr := c.errWriter
|
||||
|
||||
bb := new(bytes.Buffer)
|
||||
c.SetOutput(bb)
|
||||
c.outWriter = bb
|
||||
c.errWriter = bb
|
||||
|
||||
c.Usage()
|
||||
c.output = tmpOutput
|
||||
|
||||
// Setting things back to normal
|
||||
c.outWriter = tmpOutput
|
||||
c.errWriter = tmpErr
|
||||
|
||||
return bb.String()
|
||||
}
|
||||
|
||||
|
@ -793,6 +873,13 @@ func (c *Command) preRun() {
|
|||
}
|
||||
}
|
||||
|
||||
// ExecuteContext is the same as Execute(), but sets the ctx on the command.
|
||||
// Retrieve ctx by calling cmd.Context() inside your *Run lifecycle functions.
|
||||
func (c *Command) ExecuteContext(ctx context.Context) error {
|
||||
c.ctx = ctx
|
||||
return c.Execute()
|
||||
}
|
||||
|
||||
// Execute uses the args (os.Args[1:] by default)
|
||||
// and run through the command tree finding appropriate matches
|
||||
// for commands and then corresponding flags.
|
||||
|
@ -803,6 +890,10 @@ func (c *Command) Execute() error {
|
|||
|
||||
// ExecuteC executes the command.
|
||||
func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
if c.ctx == nil {
|
||||
c.ctx = context.Background()
|
||||
}
|
||||
|
||||
// Regardless of what command execute is called on, run on Root only
|
||||
if c.HasParent() {
|
||||
return c.Root().ExecuteC()
|
||||
|
@ -817,15 +908,16 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
|||
// overriding
|
||||
c.InitDefaultHelpCmd()
|
||||
|
||||
var args []string
|
||||
args := c.args
|
||||
|
||||
// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
|
||||
if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
|
||||
args = os.Args[1:]
|
||||
} else {
|
||||
args = c.args
|
||||
}
|
||||
|
||||
// initialize the hidden command to be used for bash completion
|
||||
c.initCompleteCmd(args)
|
||||
|
||||
var flags []string
|
||||
if c.TraverseChildren {
|
||||
cmd, flags, err = c.Traverse(args)
|
||||
|
@ -849,6 +941,12 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
|||
cmd.commandCalledAs.name = cmd.Name()
|
||||
}
|
||||
|
||||
// We have to pass global context to children command
|
||||
// if context is present on the parent command.
|
||||
if cmd.ctx == nil {
|
||||
cmd.ctx = c.ctx
|
||||
}
|
||||
|
||||
err = cmd.execute(flags)
|
||||
if err != nil {
|
||||
// Always show help if requested, even if SilenceErrors is in
|
||||
|
@ -932,7 +1030,11 @@ func (c *Command) InitDefaultVersionFlag() {
|
|||
} else {
|
||||
usage += c.Name()
|
||||
}
|
||||
c.Flags().Bool("version", false, usage)
|
||||
if c.Flags().ShorthandLookup("v") == nil {
|
||||
c.Flags().BoolP("version", "v", false, usage)
|
||||
} else {
|
||||
c.Flags().Bool("version", false, usage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1070,6 +1172,21 @@ func (c *Command) Printf(format string, i ...interface{}) {
|
|||
c.Print(fmt.Sprintf(format, i...))
|
||||
}
|
||||
|
||||
// PrintErr is a convenience method to Print to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErr(i ...interface{}) {
|
||||
fmt.Fprint(c.ErrOrStderr(), i...)
|
||||
}
|
||||
|
||||
// PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErrln(i ...interface{}) {
|
||||
c.Print(fmt.Sprintln(i...))
|
||||
}
|
||||
|
||||
// PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErrf(format string, i ...interface{}) {
|
||||
c.Print(fmt.Sprintf(format, i...))
|
||||
}
|
||||
|
||||
// CommandPath returns the full path to this command.
|
||||
func (c *Command) CommandPath() string {
|
||||
if c.HasParent() {
|
||||
|
@ -1335,7 +1452,7 @@ func (c *Command) LocalFlags() *flag.FlagSet {
|
|||
return c.lflags
|
||||
}
|
||||
|
||||
// InheritedFlags returns all flags which were inherited from parents commands.
|
||||
// InheritedFlags returns all flags which were inherited from parent commands.
|
||||
func (c *Command) InheritedFlags() *flag.FlagSet {
|
||||
c.mergePersistentFlags()
|
||||
|
||||
|
@ -1470,7 +1587,7 @@ func (c *Command) ParseFlags(args []string) error {
|
|||
beforeErrorBufLen := c.flagErrorBuf.Len()
|
||||
c.mergePersistentFlags()
|
||||
|
||||
//do it here after merging all flags and just before parse
|
||||
// do it here after merging all flags and just before parse
|
||||
c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)
|
||||
|
||||
err := c.Flags().Parse(args)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue