-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
example_dispatch_c_test.go
52 lines (45 loc) · 1.17 KB
/
example_dispatch_c_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
51
52
package getoptions_test
import (
"context"
"errors"
"fmt"
"os"
"github.com/DavidGamba/go-getoptions"
)
func ExampleGetOpt_Dispatch_cCommandHelp() {
runFn := func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
return nil
}
opt := getoptions.New()
opt.Bool("debug", false)
list := opt.NewCommand("list", "list stuff").SetCommandFn(runFn)
list.Bool("list-opt", false)
opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse([]string{"help", "list"}) // <- argv set to call command 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:
// NAME:
// go-getoptions.test list - list stuff
//
// SYNOPSIS:
// go-getoptions.test list [--debug] [--help|-?] [--list-opt] [<args>]
//
// OPTIONS:
// --debug (default: false)
//
// --help|-? (default: false)
//
// --list-opt (default: false)
//
}