feat: add a command tree help view (#32)

Add a command tree help view.

Following up #29 and #30, I noticed that the default help looks quite duplicative since only the help text of the last command in chain is viewed. I needed an option to also show the help text of the command nodes in an appropriate way.  This is what I came up with.
This commit is contained in:
Matthias Fax
2019-01-21 14:14:26 +01:00
committed by Alec Thomas
parent fcf5f9bc1a
commit 2a90ca2ad3
2 changed files with 176 additions and 10 deletions
+95
View File
@@ -124,3 +124,98 @@ Flags:
require.Equal(t, expected, w.String())
})
}
func TestHelpTree(t *testing.T) {
// nolint: govet
var cli struct {
One struct {
Thing struct {
Arg string `arg help:"argument"`
} `cmd help:"subcommand thing"`
Other struct {
Other string `arg help:"other arg"`
} `arg help:"subcommand other"`
} `cmd help:"subcommand one"`
Two struct {
Three threeArg `arg help:"Sub-sub-arg."`
Four struct {
} `cmd help:"Sub-sub-command."`
} `cmd help:"Another subcommand."`
}
w := bytes.NewBuffer(nil)
exited := false
app := mustNew(t, &cli,
kong.Name("test-app"),
kong.Description("A test app."),
kong.Writers(w, w),
kong.ConfigureHelp(kong.HelpOptions{
Tree: true,
Indenter: kong.LineIndenter,
}),
kong.Exit(func(int) {
exited = true
panic(true) // Panic to fake "exit".
}),
)
t.Run("Full", func(t *testing.T) {
require.PanicsWithValue(t, true, func() {
_, err := app.Parse([]string{"--help"})
require.NoError(t, err)
})
require.True(t, exited)
expected := `Usage: test-app <command>
A test app.
Flags:
--help Show context-sensitive help.
Command Tree:
one subcommand one
- thing subcommand thing
- <arg> argument
- <other> subcommand other
two Another subcommand.
- <three> Sub-sub-arg.
- four Sub-sub-command.
Run "test-app <command> --help" for more information on a command.
`
if expected != w.String() {
t.Errorf("help command returned:\n%v\n\nwant:\n%v", w.String(), expected)
}
require.Equal(t, expected, w.String())
})
t.Run("Selected", func(t *testing.T) {
exited = false
w.Truncate(0)
require.PanicsWithValue(t, true, func() {
_, err := app.Parse([]string{"one", "--help"})
require.NoError(t, err)
})
require.True(t, exited)
expected := `Usage: test-app one <command>
subcommand one
Flags:
--help Show context-sensitive help.
Command Tree:
thing subcommand thing
- <arg> argument
<other> subcommand other
`
if expected != w.String() {
t.Errorf("help command returned:\n%v\n\nwant:\n%v", w.String(), expected)
}
require.Equal(t, expected, w.String())
})
}