generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
61 lines (53 loc) · 1.33 KB
/
example_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
53
54
55
56
57
58
59
60
61
package climan_test
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/peterbourgon/ff/v3"
"moul.io/climan"
)
var opts struct {
debug bool
fooFlag string
}
func Example() {
root := &climan.Command{
Name: "example",
ShortUsage: "example [global flags] <subcommand> [flags] [args...]",
ShortHelp: "example's short help",
LongHelp: "example's longer help.\nwith more details.",
FlagSetBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.debug, "debug", opts.debug, "debug mode")
},
Exec: doRoot,
Subcommands: []*climan.Command{
&climan.Command{
Name: "foo",
FlagSetBuilder: func(fs *flag.FlagSet) {
fs.BoolVar(&opts.debug, "debug", opts.debug, "debug mode")
fs.StringVar(&opts.fooFlag, "flag", opts.fooFlag, "foo's flag")
},
ShortUsage: "foo [flags]",
ShortHelp: "foo things",
Exec: doFoo,
},
},
FFOptions: []ff.Option{ff.WithEnvVarPrefix("EXAMPLE")},
}
if err := root.Parse(os.Args[1:]); err != nil {
log.Fatal(fmt.Errorf("parse error: %w", err))
}
if err := root.Run(context.Background()); err != nil {
log.Fatal(fmt.Errorf("run error: %w", err))
}
}
func doRoot(ctx context.Context, args []string) error {
fmt.Println("args", args)
return nil
}
func doFoo(ctx context.Context, args []string) error {
fmt.Println("flag", opts.fooFlag)
return nil
}