Skip to content

Commit

Permalink
cli: use FlagSet output instead of mutating it
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWhited committed Dec 26, 2018
1 parent 42b7e70 commit 0fb758a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
12 changes: 8 additions & 4 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"flag"
"fmt"
"io"
"os"
"strings"
)

Expand Down Expand Up @@ -52,19 +53,22 @@ type Command struct {
}

// Help writes the usage line, flags, and description for the command to the
// provided io.Writer.
// If c.Flags is a valid flag set, calling Help sets the output of c.Flags.
func (c *Command) Help(w io.Writer) {
// flag set's output or to stdout if Flags is nil.
func (c *Command) Help() {
if c == nil {
return
}

var w io.Writer = os.Stdout
if c.Flags != nil {
w = c.Flags.Output()
}
// If there is a usage line and it's more than just the name, print it.
if c.Usage != "" && c.Name() != c.Usage {
fmt.Fprintf(w, "Usage: %s\n\n", c.Usage)
}
if c.Flags != nil {
fmt.Fprint(w, "Options:\n\n")
c.Flags.SetOutput(w)
c.Flags.PrintDefaults()
fmt.Fprintln(w, "")
}
Expand Down
11 changes: 8 additions & 3 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func TestCommand(t *testing.T) {
}
})
t.Run(fmt.Sprintf("Help/%d", i), func(t *testing.T) {
tc.cmd.Flags = flag.NewFlagSet("testflags", flag.ExitOnError)
tc.cmd.Flags = flag.NewFlagSet("testflags", flag.PanicOnError)
tc.cmd.Flags.String("testflag", "testflagvalue", "usage of a test flag")

b.Reset()
tc.cmd.Help(b)
tc.cmd.Flags.SetOutput(b)
tc.cmd.Help()
if tc.cmd.Run != nil && !bytes.Contains(b.Bytes(), []byte(tc.cmd.Usage)) {
t.Errorf("Expected cmd.Help() output to contain cmd.Usage")
}
Expand Down Expand Up @@ -125,7 +126,11 @@ func TestCommandSet(t *testing.T) {
if tc.cs != nil {
t.Run(fmt.Sprintf("Help/%d", i), func(t *testing.T) {
b := new(bytes.Buffer)
tc.cs.Help(b)
if tc.cs.Flags == nil {
tc.cs.Flags = flag.NewFlagSet("t", flag.PanicOnError)
}
tc.cs.Flags.SetOutput(b)
tc.cs.Help()
for _, cmd := range tc.cs.Commands {
if !bytes.Contains(b.Bytes(), []byte(cmd.Name())) {
t.Errorf("Expected commandset help to contain command name: %s", cmd.Name())
Expand Down
3 changes: 2 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

func commitCmd(cfg *string) *cli.Command {
commitFlags := flag.NewFlagSet("commit", flag.ExitOnError)
commitFlags.SetOutput(os.Stdout)
help := commitFlags.Bool("h", false, "Print this commands help output…")
interactive := commitFlags.Bool("interactive", false, "Run commit in interactive mode.")
if cfg == nil {
Expand All @@ -35,7 +36,7 @@ Stores the current contents of the index in a new commit…`,
fmt.Println("Interactive mode enabled.")
}
if *help {
c.Help(os.Stdout)
c.Help()
}
return nil
},
Expand Down
8 changes: 4 additions & 4 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package cli

import (
"fmt"
"os"
)

// Help returns a Command that prints help information about its command set to
// stdout, or about a specific command if one is provided as an argument.
// the command's Help output, or information about a specific command if one is
// provided as an argument.
//
// For example, in a program called "git" running:
//
Expand All @@ -24,7 +24,7 @@ func Help(cs *Command) *Command {
Run: func(c *Command, args ...string) error {
// If there aren't any arguments, print the main command help.
if len(args) == 0 {
cs.Help(os.Stdout)
cs.Help()
return nil
}

Expand All @@ -35,7 +35,7 @@ func Help(cs *Command) *Command {
}
// If this is the article, run its help command.
if len(args) == 1 {
cmd.Help(os.Stdout)
cmd.Help()
return nil
}

Expand Down

0 comments on commit 0fb758a

Please sign in to comment.