-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
79 lines (72 loc) · 1.46 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
// This packages shows how to use sflags with urfave/cli library.
import (
"fmt"
"log"
"net"
"regexp"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/urfave/cli/v2"
"github.com/urfave/sflags"
"github.com/urfave/sflags/gen/gcli"
)
type httpConfig struct {
Host string ` desc:"HTTP host"`
Port int `flag:"port p"`
SSL bool
Timeout time.Duration
Addr *net.TCPAddr
}
type config struct {
HTTP httpConfig
Regexp *regexp.Regexp
Count sflags.Counter
HiddenFlag string `flag:",hidden"`
}
func main() {
cfg := &config{
HTTP: httpConfig{
Host: "127.0.0.1",
Port: 6000,
SSL: false,
Timeout: 15 * time.Second,
Addr: &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 4000,
},
},
Count: 12,
Regexp: regexp.MustCompile("abc"),
}
flags, err := gcli.Parse(cfg)
if err != nil {
log.Fatalf("err: %v", err)
}
cliApp := cli.NewApp()
cliApp.Action = func(c *cli.Context) error {
return nil
}
cliApp.Flags = flags
// print usage
err = cliApp.Run([]string{"cliApp", "--help"})
if err != nil {
fmt.Printf("err: %v", err)
}
err = cliApp.Run([]string{
"cliApp",
"--count=10",
"--http-host", "localhost",
"-p", "9000",
"--http-ssl",
"--http-timeout", "30s",
"--http-addr", "google.com:8000",
"--regexp", "ddfd",
"--count", "--count",
"--hidden-flag", "hidden_value",
})
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Printf("\ncfg: %s\n", spew.Sdump(cfg))
}