-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag.go
38 lines (32 loc) · 707 Bytes
/
flag.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
package main
import (
"flag"
"fmt"
)
// XDG_STATE_HOME
const rootFlagKey = "test.r"
func NewFlagSet(name, args string) *flag.FlagSet {
f := flag.NewFlagSet(name, flag.ExitOnError)
f.Usage = func() {
fmt.Fprintf(f.Output(), "usage: %s [options]", name)
if args != "" {
fmt.Fprintf(f.Output(), " %s", args)
}
fmt.Fprintln(f.Output(), "\nFlags:")
f.PrintDefaults()
}
if flag.Lookup("test.v") != nil {
f.String(rootFlagKey, "", "root `dir`ectory")
}
return f
}
func RootDir(f *flag.FlagSet) string {
root := f.Lookup(rootFlagKey)
if root != nil && root.Value.String() != "" {
return root.Value.String()
}
return defaultRootDir()
}
func defaultRootDir() string {
return "."
}