-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
45 lines (35 loc) · 951 Bytes
/
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
package main
import (
"fmt"
"os"
flags "github.com/jessevdk/go-flags"
)
type config struct {
Address string `short:"a" long:"address" description:"Address to bind to" default:"127.0.0.1" value-name:"ADDRESS"`
File string `short:"f" long:"file" description:"File to serve (json format)" default:"metadata.json" value-name:"PATH_TO_FILE"`
Port string `short:"p" long:"port" description:"Port to bind to" default:"8080" value-name:"PORT"`
Version func() `short:"v" long:"version" description:"Display the version"`
}
func parseCliArgs() *config {
c := &config{}
c.Version = func() {
fmt.Println(version)
os.Exit(0)
}
parser := flags.NewParser(c, flags.Default)
args, err := parser.Parse()
if err != nil {
helpDisplayed := false
for _, i := range args {
if i == "-h" || i == "--help" {
helpDisplayed = true
break
}
}
if !helpDisplayed {
parser.WriteHelp(os.Stderr)
}
os.Exit(1)
}
return c
}