summaryrefslogtreecommitdiff
path: root/example_test.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2021-09-18 08:57:29 -0700
committerGitHub <[email protected]>2021-09-18 08:57:29 -0700
commita4afd6a8490903fdc93b319be0d54bd2039c3f08 (patch)
tree8441653724d058566ba492200900a5c6cca84d17 /example_test.go
parent66cb696e79be30bc979cb003ef8bdecc78266ced (diff)
parentf2f876420ce7c64673cdf97ed8071db8ef46e5f2 (diff)
Merge pull request #156 from alexflint/usage-for-subcommands
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 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