vendor/github.com/spf13/cobra/command.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
child 265 05c40b36d3b2
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    16 package cobra
    16 package cobra
    17 
    17 
    18 import (
    18 import (
    19 	"bytes"
    19 	"bytes"
    20 	"context"
    20 	"context"
       
    21 	"errors"
    21 	"fmt"
    22 	"fmt"
    22 	"io"
    23 	"io"
    23 	"os"
    24 	"os"
    24 	"path/filepath"
    25 	"path/filepath"
    25 	"sort"
    26 	"sort"
   164 	// outWriter is a writer defined by the user that replaces stdout
   165 	// outWriter is a writer defined by the user that replaces stdout
   165 	outWriter io.Writer
   166 	outWriter io.Writer
   166 	// errWriter is a writer defined by the user that replaces stderr
   167 	// errWriter is a writer defined by the user that replaces stderr
   167 	errWriter io.Writer
   168 	errWriter io.Writer
   168 
   169 
   169 	//FParseErrWhitelist flag parse errors to be ignored
   170 	// FParseErrWhitelist flag parse errors to be ignored
   170 	FParseErrWhitelist FParseErrWhitelist
   171 	FParseErrWhitelist FParseErrWhitelist
   171 
   172 
   172 	// CompletionOptions is a set of options to control the handling of shell completion
   173 	// CompletionOptions is a set of options to control the handling of shell completion
   173 	CompletionOptions CompletionOptions
   174 	CompletionOptions CompletionOptions
   174 
   175 
   222 	// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.
   223 	// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.
   223 	// Must be > 0.
   224 	// Must be > 0.
   224 	SuggestionsMinimumDistance int
   225 	SuggestionsMinimumDistance int
   225 }
   226 }
   226 
   227 
   227 // Context returns underlying command context. If command wasn't
   228 // Context returns underlying command context. If command was executed
   228 // executed with ExecuteContext Context returns Background context.
   229 // with ExecuteContext or the context was set with SetContext, the
       
   230 // previously set context will be returned. Otherwise, nil is returned.
       
   231 //
       
   232 // Notice that a call to Execute and ExecuteC will replace a nil context of
       
   233 // a command with a context.Background, so a background context will be
       
   234 // returned by Context after one of these functions has been called.
   229 func (c *Command) Context() context.Context {
   235 func (c *Command) Context() context.Context {
   230 	return c.ctx
   236 	return c.ctx
       
   237 }
       
   238 
       
   239 // SetContext sets context for the command. It is set to context.Background by default and will be overwritten by
       
   240 // Command.ExecuteContext or Command.ExecuteContextC
       
   241 func (c *Command) SetContext(ctx context.Context) {
       
   242 	c.ctx = ctx
   231 }
   243 }
   232 
   244 
   233 // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
   245 // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
   234 // particularly useful when testing.
   246 // particularly useful when testing.
   235 func (c *Command) SetArgs(a []string) {
   247 func (c *Command) SetArgs(a []string) {
   850 	}
   862 	}
   851 
   863 
   852 	if err := c.validateRequiredFlags(); err != nil {
   864 	if err := c.validateRequiredFlags(); err != nil {
   853 		return err
   865 		return err
   854 	}
   866 	}
       
   867 	if err := c.validateFlagGroups(); err != nil {
       
   868 		return err
       
   869 	}
       
   870 
   855 	if c.RunE != nil {
   871 	if c.RunE != nil {
   856 		if err := c.RunE(c, argWoFlags); err != nil {
   872 		if err := c.RunE(c, argWoFlags); err != nil {
   857 			return err
   873 			return err
   858 		}
   874 		}
   859 	} else {
   875 	} else {
   973 
   989 
   974 	err = cmd.execute(flags)
   990 	err = cmd.execute(flags)
   975 	if err != nil {
   991 	if err != nil {
   976 		// Always show help if requested, even if SilenceErrors is in
   992 		// Always show help if requested, even if SilenceErrors is in
   977 		// effect
   993 		// effect
   978 		if err == flag.ErrHelp {
   994 		if errors.Is(err, flag.ErrHelp) {
   979 			cmd.HelpFunc()(cmd, args)
   995 			cmd.HelpFunc()(cmd, args)
   980 			return cmd, nil
   996 			return cmd, nil
   981 		}
   997 		}
   982 
   998 
   983 		// If root command has SilenceErrors flagged,
   999 		// If root command has SilenceErrors flagged,
   995 	return cmd, err
  1011 	return cmd, err
   996 }
  1012 }
   997 
  1013 
   998 func (c *Command) ValidateArgs(args []string) error {
  1014 func (c *Command) ValidateArgs(args []string) error {
   999 	if c.Args == nil {
  1015 	if c.Args == nil {
  1000 		return nil
  1016 		return ArbitraryArgs(c, args)
  1001 	}
  1017 	}
  1002 	return c.Args(c, args)
  1018 	return c.Args(c, args)
  1003 }
  1019 }
  1004 
  1020 
  1005 func (c *Command) validateRequiredFlags() error {
  1021 func (c *Command) validateRequiredFlags() error {