-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathargs.go
45 lines (37 loc) · 923 Bytes
/
args.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 (
"flag"
"fmt"
"os"
"path/filepath"
)
// Args are command line arguments.
type Args struct {
ConfigFile string
ListenFD int
}
func getArgs() *Args {
configFile := flag.String("conf", "", "Configuration file.")
fd := flag.Int("listen-fd", -1,
"File descriptor with listening port to use (optional).")
flag.Parse()
if len(*configFile) == 0 {
printUsage(fmt.Errorf("you must provide a configuration file"))
return nil
}
configPath, err := filepath.Abs(*configFile)
if err != nil {
printUsage(fmt.Errorf(
"unable to determine path to the configuration file: %s", err))
return nil
}
return &Args{
ConfigFile: configPath,
ListenFD: *fd,
}
}
func printUsage(err error) {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err) // nolint: gas
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s <arguments>\n", os.Args[0]) // nolint: gas
flag.PrintDefaults()
}