Skip to content

Commit

Permalink
Adding support for --help
Browse files Browse the repository at this point in the history
  • Loading branch information
spf13 committed Jun 17, 2014
1 parent 3e874b3 commit 07be814
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ embeds the usage as part of it's output.
hugo [command]

Available Commands:
server :: Hugo runs it's own a webserver to render the files
version :: Print the version number of Hugo
check :: Check content in the source directory
benchmark :: Benchmark hugo by building a site a number of times
help [command] :: Help about any command
server Hugo runs it's own a webserver to render the files
version Print the version number of Hugo
check Check content in the source directory
benchmark Benchmark hugo by building a site a number of times
help [command] Help about any command

Available Flags:
-b, --base-url="": hostname (and path) to the root eg. http://spf13.com/
Expand Down
63 changes: 56 additions & 7 deletions cobra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,49 @@ func initializeWithRootCmd() *Command {
tt, tp, te, rootcalled = nil, nil, nil, false
flagInit()
cmdRootWithRun.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")
cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag intthree")
cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag introot")
commandInit()
return cmdRootWithRun
}

type resulter struct {
Error error
Output string
Command *Command
}

func fullSetupTest(input string) resulter {
c := initializeWithRootCmd()

return fullTester(c, input)
}

func noRRSetupTest(input string) resulter {
c := initialize()

return fullTester(c, input)
}

func fullTester(c *Command, input string) resulter {
buf := new(bytes.Buffer)
// Testing flag with invalid input
c.SetOutput(buf)
cmdEcho.AddCommand(cmdTimes)
c.AddCommand(cmdPrint, cmdEcho)
c.SetArgs(strings.Split(input, " "))

err := c.Execute()
output := buf.String()

return resulter{err, output, c}
}

func checkResultContains(t *testing.T, x resulter, check string) {
if !strings.Contains(x.Output, check) {
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, x.Output)
}
}

func checkOutputContains(t *testing.T, c *Command, check string) {
buf := new(bytes.Buffer)
c.SetOutput(buf)
Expand Down Expand Up @@ -448,12 +486,23 @@ func TestRootFlags(t *testing.T) {
}

func TestRootHelp(t *testing.T) {
fmt.Println("testing root help")
c := initializeWithRootCmd()
c.AddCommand(cmdPrint, cmdEcho)
c.SetArgs(strings.Split("--help", " "))
e := c.Execute()
fmt.Println(e)
x := fullSetupTest("--help")

checkResultContains(t, x, "Available Commands:")

if strings.Contains(x.Output, "unknown flag: --help") {
t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
}

x = fullSetupTest("echo --help")

checkResultContains(t, x, "Available Commands:")

if strings.Contains(x.Output, "unknown flag: --help") {
t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
}

}

checkOutputContains(t, c, "Available Commands:")
}
16 changes: 15 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Command struct {
helpTemplate string // Can be defined by Application
helpFunc func(*Command, []string) // Help can be defined by application
helpCommand *Command // The help command
helpFlagVal bool
}

// os.Args[1:] by default, if desired, can be overridden
Expand Down Expand Up @@ -301,6 +302,12 @@ func (c *Command) execute(a []string) (err error) {
if err != nil {
return err
} else {
// If help is called, regardless of other flags, we print that
if c.helpFlagVal {
c.Help()
return nil
}

argWoFlags := c.Flags().Args()
c.Run(c, argWoFlags)
return nil
Expand Down Expand Up @@ -349,6 +356,13 @@ func (c *Command) Execute() (err error) {
c.Usage()
return e
} else {

// If help is called, regardless of other flags, we print that
if c.helpFlagVal {
c.Help()
return nil
}

argWoFlags := c.Flags().Args()
if len(argWoFlags) > 0 {
c.Usage()
Expand Down Expand Up @@ -555,6 +569,7 @@ func (c *Command) Flags() *flag.FlagSet {
c.flagErrorBuf = new(bytes.Buffer)
}
c.flags.SetOutput(c.flagErrorBuf)
c.flags.BoolVar(&c.helpFlagVal, "help", false, "help for "+c.Name())
}
return c.flags
}
Expand Down Expand Up @@ -617,7 +632,6 @@ func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
// Parses persistent flag tree & local flags
func (c *Command) ParseFlags(args []string) (err error) {
c.mergePersistentFlags()

err = c.Flags().Parse(args)

// The upstream library adds spaces to the error
Expand Down

0 comments on commit 07be814

Please sign in to comment.