-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
example_dispatch_b_test.go
50 lines (43 loc) · 1.16 KB
/
example_dispatch_b_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package getoptions_test
import (
"context"
"errors"
"fmt"
"os"
"github.com/DavidGamba/go-getoptions"
)
func ExampleGetOpt_Dispatch_bHelp() {
runFn := func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
return nil
}
opt := getoptions.New()
opt.Bool("debug", false)
opt.NewCommand("list", "list stuff").SetCommandFn(runFn)
opt.HelpCommand("help", opt.Alias("?"), opt.Description("Show this help"))
remaining, err := opt.Parse([]string{"help"}) // <- argv set to call help
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
getoptions.Writer = os.Stdout // Print help to stdout instead of stderr for test purpose
err = opt.Dispatch(context.Background(), remaining)
if err != nil {
if !errors.Is(err, getoptions.ErrorHelpCalled) {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}
// Output:
// SYNOPSIS:
// go-getoptions.test [--debug] [--help|-?] <command> [<args>]
//
// COMMANDS:
// list list stuff
//
// OPTIONS:
// --debug (default: false)
//
// --help|-? Show this help (default: false)
//
// Use 'go-getoptions.test help <command>' for extra details.
}