summaryrefslogtreecommitdiff
path: root/example_test.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2021-05-09 13:55:34 -0700
committerAlex Flint <[email protected]>2021-05-09 13:55:34 -0700
commitc9b504edc1b8da6b21564c1ebf5c1b49ed9ff431 (patch)
tree7b6c4e6bdb5e381f37d23c6107d2e9a5b0ab3941 /example_test.go
parent679be43af38a865f8318ed9bc3fde7ae424a2dad (diff)
add FailSubcommand, WriteUsageForSubcommand, WriteHelpForSubcommand
Diffstat (limited to 'example_test.go')
-rw-r--r--example_test.go95
1 files changed, 94 insertions, 1 deletions
diff --git a/example_test.go b/example_test.go
index 2e9b875..2b7ce00 100644
--- a/example_test.go
+++ b/example_test.go
@@ -250,7 +250,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")
@@ -286,6 +286,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