-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
76 lines (67 loc) · 1.48 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
package main
// This packages shows how to use sflags with kingpin library.
import (
"fmt"
"log"
"net"
"regexp"
"time"
"github.com/alecthomas/kingpin"
"github.com/davecgh/go-spew/spew"
"github.com/octago/sflags"
"github.com/octago/sflags/gen/gkingpin"
)
type httpConfig struct {
Host string `desc:"HTTP host2"`
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"),
}
app := kingpin.New("testApp", "")
app.Terminate(nil)
err := gkingpin.ParseTo(cfg, app, sflags.Prefix("kingpin."))
if err != nil {
log.Fatalf("err: %v", err)
}
// print usage
_, err = app.Parse([]string{"--help"})
if err != nil {
fmt.Printf("err: %v", err)
}
_, err = app.Parse([]string{
"--kingpin.http-host", "localhost",
"-p", "9000",
"--kingpin.http-ssl",
"--kingpin.http-timeout", "30s",
"--kingpin.http-addr", "google.com:8000",
"--kingpin.regexp", "ddfd",
"--kingpin.count", "--kingpin.count",
"--kingpin.hidden-flag", "hidden_value",
})
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Printf("\ncfg: %s\n", spew.Sdump(cfg))
}