-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
73 lines (67 loc) · 1.4 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
package main
// This packages shows how to use sflags with flag library.
import (
"fmt"
"log"
"net"
"os"
"regexp"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/urfave/sflags"
"github.com/urfave/sflags/gen/gflag"
)
type httpConfig struct {
Host string `desc:"HTTP host"`
Port int
SSL bool
Timeout time.Duration
Addr *net.TCPAddr
Methods map[string]int64 `desc:"HTTP Methods"`
}
type config struct {
HTTP httpConfig
Regexp *regexp.Regexp
Count sflags.Counter
}
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,
},
},
Regexp: regexp.MustCompile("smth"),
Count: 10,
}
// Use gflags.ParseToDef if you want default `flag.CommandLine`
fs, err := gflag.Parse(cfg)
if err != nil {
log.Fatalf("err: %v", err)
}
// You should run fs.Parse(os.Args[1:]), but this is an example.
err = fs.Parse([]string{
"-count=20",
"-http-host", "localhost",
"-http-port", "9000",
"-http-ssl",
"-http-timeout", "30s",
"-http-addr", "google.com:8000",
"-regexp", "ddfd",
"-count", "-count",
"-http-methods", "post:15",
"-http-methods", "get:25",
})
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Println("Usage:")
fs.SetOutput(os.Stdout)
fs.PrintDefaults()
fmt.Printf("cfg: %s\n", spew.Sdump(cfg))
}