Skip to content

Commit

Permalink
chore(cmd): print shortHelp if longHelp is empty (gnolang#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle authored Aug 3, 2023
1 parent 3ade982 commit 1fc25f9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newDocCmd(io *commands.IO) *commands.Command {
commands.Metadata{
Name: "doc",
ShortUsage: "doc [flags] <pkgsym>",
ShortHelp: "get documentation for the specified package or symbol (type, function, method, or variable/constant).",
ShortHelp: "get documentation for the specified package or symbol (type, function, method, or variable/constant)",
},
c,
func(_ context.Context, args []string) error {
Expand Down
2 changes: 2 additions & 0 deletions tm2/pkg/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ func usage(c *Command) string {

if c.longHelp != "" {
fmt.Fprintf(&b, "%s\n\n", c.longHelp)
} else if c.shortHelp != "" {
fmt.Fprintf(&b, "%s.\n\n", c.shortHelp)
}

if len(c.subcommands) > 0 {
Expand Down
84 changes: 71 additions & 13 deletions tm2/pkg/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,21 @@ func TestHelpUsage(t *testing.T) {
var buf bytes.Buffer
fs.SetOutput(&buf)

command := &Command{
name: "TestHelpUsage",
shortUsage: "TestHelpUsage [flags] <args>",
shortHelp: "Some short help.",
longHelp: "Some long help.",
flagSet: fs,
}

err := command.ParseAndRun(context.Background(), []string{"-h"})
assert.ErrorIs(t, err, flag.ErrHelp)
expectedOutput := strings.TrimSpace(`
tests := []struct {
name string
command *Command
expectedOutput string
}{
{
name: "normal case",
command: &Command{
name: "TestHelpUsage",
shortUsage: "TestHelpUsage [flags] <args>",
shortHelp: "Some short help",
longHelp: "Some long help.",
flagSet: fs,
},
expectedOutput: strings.TrimSpace(`
USAGE
TestHelpUsage [flags] <args>
Expand All @@ -443,8 +447,62 @@ FLAGS
-i 0 int
-s ... string
-x ... collection of strings (repeatable)
`) + "\n\n"
assert.Equal(t, expectedOutput, buf.String())
`) + "\n\n",
},
{
name: "no long help",
command: &Command{
name: "TestHelpUsage",
shortUsage: "TestHelpUsage [flags] <args>",
shortHelp: "Some short help",
flagSet: fs,
},
expectedOutput: strings.TrimSpace(`
USAGE
TestHelpUsage [flags] <args>
Some short help.
FLAGS
-b=false bool
-d 0s time.Duration
-f 0 float64
-i 0 int
-s ... string
-x ... collection of strings (repeatable)
`) + "\n\n",
},
{
name: "no short and no long help",
command: &Command{
name: "TestHelpUsage",
shortUsage: "TestHelpUsage [flags] <args>",
flagSet: fs,
},
expectedOutput: strings.TrimSpace(`
USAGE
TestHelpUsage [flags] <args>
FLAGS
-b=false bool
-d 0s time.Duration
-f 0 float64
-i 0 int
-s ... string
-x ... collection of strings (repeatable)
`) + "\n\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf.Reset()

err := tt.command.ParseAndRun(context.Background(), []string{"-h"})

assert.ErrorIs(t, err, flag.ErrHelp)
assert.Equal(t, tt.expectedOutput, buf.String())
})
}
}

// Forked from peterbourgon/ff/ffcli
Expand Down

0 comments on commit 1fc25f9

Please sign in to comment.