Improved documentation and help.

This commit is contained in:
Alec Thomas
2018-06-05 10:32:23 +10:00
parent 2afd4ba47b
commit 96fa9c43d5
15 changed files with 457 additions and 173 deletions
+56 -19
View File
@@ -9,29 +9,48 @@ import (
func TestHelp(t *testing.T) {
var cli struct {
String string `help:"A string flag."`
Bool bool `help:"A bool flag with very long help that wraps a lot and is verbose and is really verbose."`
String string `help:"A string flag."`
Bool bool `help:"A bool flag with very long help that wraps a lot and is verbose and is really verbose."`
Required bool `required help:"A required flag."`
One struct {
Flag string `help:"Nested flag."`
} `cmd help:"A subcommand."`
Two struct {
Flag string `help:"Nested flag under two."`
Flag string `help:"Nested flag under two."`
RequiredTwo bool `required`
Three struct {
RequiredThree bool `required`
Three string `arg`
} `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,
Name("test-app"),
Description("A test app."),
Writers(w, w),
ExitFunction(func(int) { exited = true }),
ExitFunction(func(int) {
exited = true
panic(true) // Panic to fake "exit".
}),
)
_, err := app.Parse([]string{"--help"})
require.NoError(t, err)
require.True(t, exited)
require.Equal(t, `usage: test-app [<flags>]
t.Run("Full", func(t *testing.T) {
require.Panics(t, func() {
_, err := app.Parse([]string{"--help"})
require.NoError(t, err)
})
require.True(t, exited)
t.Log(w.String())
require.Equal(t, `usage: test-app --required [<flags>]
A test app.
@@ -40,25 +59,43 @@ Flags:
--string=STRING A string flag.
--bool A bool flag with very long help that wraps a lot and is
verbose and is really verbose.
--required A required flag.
Commands:
one [<flags>]
one --required [<flags>]
A subcommand.
two [<flags>]
Another subcommand.
two <three> --required --required-two --required-three [<flags>]
Sub-sub-arg.
two four --required --required-two [<flags>]
Sub-sub-command.
`, w.String())
})
exited = false
w.Truncate(0)
_, err = app.Parse([]string{"one", "--help"})
require.NoError(t, err)
require.True(t, exited)
require.Equal(t, `usage: test-app one [<flags>]
t.Run("Selected", func(t *testing.T) {
exited = false
w.Truncate(0)
require.Panics(t, func() {
_, err := app.Parse([]string{"two", "hello", "--help"})
require.NoError(t, err)
})
require.True(t, exited)
t.Log(w.String())
require.Equal(t, `usage: test-app two <three> --required --required-two --required-three [<flags>]
A subcommand.
Sub-sub-arg.
Flags:
--flag=STRING Nested flag.
--string=STRING A string flag.
--bool A bool flag with very long help that wraps a lot and is
verbose and is really verbose.
--required A required flag.
--flag=STRING Nested flag under two.
--required-two
--required-three
`, w.String())
})
}