-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.go
127 lines (106 loc) · 2.28 KB
/
cli.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"os"
"time"
"github.com/champii/go-dht/dht"
"github.com/urfave/cli"
)
func prepareArgs() *cli.App {
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{if .VisibleFlags}}{{.HelpName}} [options]{{end}}
{{if len .Authors}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
VERSION:
{{.Version}}
OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Copyright }}
COPYRIGHT:
{{.Copyright}}
{{end}}{{if .Version}}
{{end}}`
cli.VersionFlag = cli.BoolFlag{
Name: "V, version",
Usage: "Print version",
}
cli.HelpFlag = cli.BoolFlag{
Name: "h, help",
Usage: "Print help",
}
app := cli.NewApp()
app.Name = "DHT"
app.Version = "0.2.0"
app.Compiled = time.Now()
app.Usage = "Experimental Distributed Hash Table"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "c, connect",
Usage: "Connect to bootstrap node ip:port",
},
cli.StringFlag{
Name: "l, listen",
Usage: "Listening address and port",
Value: ":3000",
},
cli.BoolFlag{
Name: "i, interactif",
Usage: "Interactif",
},
cli.BoolFlag{
Name: "s, store",
Usage: "Store from Stdin",
},
cli.StringFlag{
Name: "S, store-at",
Usage: "Same as '-s' but store at given `key`",
},
cli.StringFlag{
Name: "f, fetch",
Usage: "Fetch `hash` and prints to Stdout",
},
cli.StringFlag{
Name: "F, fetch-at",
Usage: "Same as '-f' but fetch from given `key`",
},
cli.IntFlag{
Name: "n, network",
Value: 0,
Usage: "Spawn X new `nodes` in a network.",
},
cli.IntFlag{
Name: "v, verbose",
Value: 3,
Usage: "Verbose `level`, 0 for CRITICAL and 5 for DEBUG",
},
}
app.UsageText = "dht [options]"
return app
}
func parseArgs(done func(dht.DhtOptions, *cli.Context)) {
app := prepareArgs()
app.Action = func(c *cli.Context) error {
options := dht.DhtOptions{
ListenAddr: c.String("l"),
BootstrapAddr: c.String("c"),
Verbose: c.Int("v"),
Stats: c.Bool("s"),
Interactif: c.Bool("i"),
Cluster: c.Int("n"),
// OnStore: func(dht.Packet) interface{} {},
}
if options.Cluster > 0 {
options.Stats = false
options.Interactif = false
}
if options.Interactif {
options.Stats = false
}
done(options, c)
return nil
}
app.Run(os.Args)
}