diff --git a/args.go b/args.go index c4d820b8..e5a34c16 100644 --- a/args.go +++ b/args.go @@ -4,6 +4,7 @@ import ( "fmt" ) +// PositionalArgs defines positional arguments callback type PositionalArgs func(cmd *Command, args []string) error // Legacy arg validation has the following behaviour: diff --git a/cobra.go b/cobra.go index 6505c070..d01becc8 100644 --- a/cobra.go +++ b/cobra.go @@ -52,7 +52,7 @@ var EnableCommandSorting = true // if the CLI is started from explorer.exe. // To disable the mousetrap, just set this variable to blank string (""). // Works only on Microsoft Windows. -var MousetrapHelpText string = `This is a command line tool. +var MousetrapHelpText = `This is a command line tool. You need to open cmd.exe and run it from there. ` @@ -61,7 +61,7 @@ You need to open cmd.exe and run it from there. // if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed. // To disable the mousetrap, just set MousetrapHelpText to blank string (""). // Works only on Microsoft Windows. -var MousetrapDisplayDuration time.Duration = 5 * time.Second +var MousetrapDisplayDuration = 5 * time.Second // AddTemplateFunc adds a template function that's available to Usage and Help // template generation. diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index dd2f7ea2..a71cae31 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -2,9 +2,10 @@ package cmd import ( "fmt" - "github.com/spf13/cobra/cobra/tpl" "os" "text/template" + + "github.com/spf13/cobra/cobra/tpl" ) // Project contains name, license and paths to projects. @@ -18,14 +19,15 @@ type Project struct { AppName string } +// Command structure type Command struct { CmdName string CmdParent string *Project } +// Create project receiver func (p *Project) Create() error { - // check if AbsolutePath exists if _, err := os.Stat(p.AbsolutePath); os.IsNotExist(err) { // create directory @@ -80,6 +82,7 @@ func (p *Project) createLicenseFile() error { return licenseTemplate.Execute(licenseFile, data) } +// Create command receiver func (c *Command) Create() error { cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName)) if err != nil { diff --git a/cobra/tpl/main.go b/cobra/tpl/main.go index 5e5a0fae..f6fe8977 100644 --- a/cobra/tpl/main.go +++ b/cobra/tpl/main.go @@ -1,5 +1,6 @@ package tpl +// MainTemplate defines main template string func MainTemplate() []byte { return []byte(`/* {{ .Copyright }} @@ -15,6 +16,7 @@ func main() { `) } +// RootTemplate defines root template string func RootTemplate() []byte { return []byte(`/* {{ .Copyright }} @@ -108,6 +110,7 @@ func initConfig() { `) } +// AddCommandTemplate defines add command template string func AddCommandTemplate() []byte { return []byte(`/* {{ .Project.Copyright }} diff --git a/command.go b/command.go index 05fd9f34..ca6dc139 100644 --- a/command.go +++ b/command.go @@ -28,8 +28,8 @@ import ( flag "github.com/spf13/pflag" ) -// NotRunnable defines subcommand error -var NotRunnable = errors.New("subcommand is required") +// ErrSubCommandRequired defines subcommand error +var ErrSubCommandRequired = errors.New("subcommand is required") // FParseErrWhitelist configures Flag parse errors to be ignored type FParseErrWhitelist flag.ParseErrorsWhitelist @@ -232,7 +232,7 @@ func (c *Command) SetErr(newErr io.Writer) { c.errWriter = newErr } -// SetOut sets the source for input data +// 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 @@ -301,7 +301,7 @@ func (c *Command) ErrOrStderr() io.Writer { return c.getErr(os.Stderr) } -// ErrOrStderr returns output to stderr +// InOrStdin returns output to stderr func (c *Command) InOrStdin() io.Reader { return c.getIn(os.Stdin) } @@ -790,7 +790,7 @@ func (c *Command) execute(a []string) (err error) { } if !c.Runnable() { - return NotRunnable + return ErrSubCommandRequired } c.preRun() @@ -927,7 +927,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { // If command wasn't runnable, show full help, but do return the error. // This will result in apps by default returning a non-success exit code, but also gives them the option to // handle specially. - if err == NotRunnable { + if err == ErrSubCommandRequired { cmd.HelpFunc()(cmd, args) return cmd, err } @@ -947,6 +947,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { return cmd, err } +// ValidateArgs validates arguments func (c *Command) ValidateArgs(args []string) error { if c.Args == nil { return nil diff --git a/command_test.go b/command_test.go index e7961c8f..b26bd4ab 100644 --- a/command_test.go +++ b/command_test.go @@ -836,7 +836,7 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) { rootCmd.AddCommand(childCmd) output, err := executeCommand(rootCmd, "child") - if err != NotRunnable { + if err != ErrSubCommandRequired { t.Errorf("Expected error") }