diff options
| author | Dylan Allbee <[email protected]> | 2020-01-23 21:09:21 -0800 |
|---|---|---|
| committer | Dylan Allbee <[email protected]> | 2020-01-25 11:53:34 -0800 |
| commit | 5df19ebe00e88d443e062c6661f52b7069f486d7 (patch) | |
| tree | c9f7cdbf1e8d9b1163d1a0a1f4198ccfd9f18537 /usage_test.go | |
| parent | 338e831a8474c442b2c5e75eefed7b602276faa8 (diff) | |
Use command passed into p.Parse(...) write methods
It is currently impossible to programatically write help and usage
messages for subcommands, due to parser.WriteHelp and parser.WriteUsage
not taking the state of the parser into account.
Check for the existence of p.lastCmd and use it for the writers when
available.
Enables ability to write unit tests for subcommand help.
Diffstat (limited to 'usage_test.go')
| -rw-r--r-- | usage_test.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/usage_test.go b/usage_test.go index d9d33f0..24846ae 100644 --- a/usage_test.go +++ b/usage_test.go @@ -266,8 +266,45 @@ Options: p, err := NewParser(Config{}, &args) require.NoError(t, err) + var help bytes.Buffer + p.WriteHelp(&help) + assert.Equal(t, expectedHelp, help.String()) +} + +func TestUsagWithNestedSubcommands(t *testing.T) { + expectedHelp := `Usage: example child nested [--enable] OUTPUT + +Positional arguments: + OUTPUT + +Options: + --enable + +Global options: + --values VALUES Values + --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"` + Nested *struct { + Enable bool + Output string `arg:"positional,required"` + } `arg:"subcommand:nested"` + } `arg:"subcommand:child"` + } + os.Args[0] = "example" + p, err := NewParser(Config{}, &args) + require.NoError(t, err) + + err = p.Parse([]string{"child", "nested", "value"}) + var help bytes.Buffer p.WriteHelp(&help) + fmt.Println(help.String()) assert.Equal(t, expectedHelp, help.String()) } |
