diff options
Diffstat (limited to 'example_test.go')
| -rw-r--r-- | example_test.go | 95 |
1 files changed, 94 insertions, 1 deletions
diff --git a/example_test.go b/example_test.go index 8394289..20b9225 100644 --- a/example_test.go +++ b/example_test.go @@ -254,7 +254,7 @@ func Example_helpTextWithSubcommand() { } // This example shows the usage string generated by go-arg when using subcommands -func Example_helpTextForSubcommand() { +func Example_helpTextWhenUsingSubcommand() { // These are the args you would pass in on the command line os.Args = split("./example get --help") @@ -290,6 +290,99 @@ func Example_helpTextForSubcommand() { // --help, -h display this help and exit } +// This example shows how to print help for an explicit subcommand +func Example_writeHelpForSubcommand() { + // These are the args you would pass in on the command line + os.Args = split("./example get --help") + + type getCmd struct { + Item string `arg:"positional" help:"item to fetch"` + } + + type listCmd struct { + Format string `help:"output format"` + Limit int + } + + var args struct { + Verbose bool + Get *getCmd `arg:"subcommand" help:"fetch an item and print it"` + List *listCmd `arg:"subcommand" help:"list available items"` + } + + // This is only necessary when running inside golang's runnable example harness + osExit = func(int) {} + stdout = os.Stdout + + p, err := NewParser(Config{}, &args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + err = p.WriteHelpForSubcommand(os.Stdout, "list") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // output: + // Usage: example list [--format FORMAT] [--limit LIMIT] + // + // Options: + // --format FORMAT output format + // --limit LIMIT + // + // Global options: + // --verbose + // --help, -h display this help and exit +} + +// This example shows how to print help for a subcommand that is nested several levels deep +func Example_writeHelpForSubcommandNested() { + // These are the args you would pass in on the command line + os.Args = split("./example get --help") + + type mostNestedCmd struct { + Item string + } + + type nestedCmd struct { + MostNested *mostNestedCmd `arg:"subcommand"` + } + + type topLevelCmd struct { + Nested *nestedCmd `arg:"subcommand"` + } + + var args struct { + TopLevel *topLevelCmd `arg:"subcommand"` + } + + // This is only necessary when running inside golang's runnable example harness + osExit = func(int) {} + stdout = os.Stdout + + p, err := NewParser(Config{}, &args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + err = p.WriteHelpForSubcommand(os.Stdout, "toplevel", "nested", "mostnested") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // output: + // Usage: example toplevel nested mostnested [--item ITEM] + // + // Options: + // --item ITEM + // --help, -h display this help and exit +} + // This example shows the error string generated by go-arg when an invalid option is provided func Example_errorText() { // These are the args you would pass in on the command line |
