Skip to content

Commit

Permalink
add a test case with single-level subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
alexflint committed Apr 2, 2024
1 parent 3ddfffd commit 8a91726
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,50 @@ Options:
assert.Equal(t, expectedUsage, usage.String())
}

func TestUsageWithSubcommands(t *testing.T) {
expectedUsage := "Usage: example child [--values VALUES]"

expectedHelp := `
Usage: example child [--values VALUES]
Options:
--values VALUES Values
Global options:
--verbose, -v verbosity level
--help, -h display this help and exit
`

var args struct {
Verbose bool `arg:"-v" help:"verbosity level"`
Child *struct {
Values []float64 `help:"Values"`
} `arg:"subcommand:child"`
}

os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)

_ = p.Parse([]string{"child"})

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

var help2 bytes.Buffer
p.WriteHelpForSubcommand(&help2, "child")
assert.Equal(t, expectedHelp[1:], help2.String())

var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))

var usage2 bytes.Buffer
p.WriteUsageForSubcommand(&usage2, "child")
assert.Equal(t, expectedUsage, strings.TrimSpace(usage2.String()))
}

func TestUsageWithNestedSubcommands(t *testing.T) {
expectedUsage := "Usage: example child nested [--enable] OUTPUT"

Expand Down

0 comments on commit 8a91726

Please sign in to comment.