-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharg.go
97 lines (78 loc) · 1.75 KB
/
arg.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
package main
import (
"fmt"
"os"
"runtime"
"runtime/debug"
flag "github.com/spf13/pflag"
)
const (
cmdName string = "highlightrepo"
exitOK int = 0
exitErr int = 1
)
var (
version = ""
installFrom = "Source"
)
type options struct {
color string
noTilde bool
nonTTY bool
}
func (cli *runner) parseArgs() *options {
o := &options{}
var flagHelp bool
var flagVersion bool
flag.StringVarP(&o.color, "color", "c", "cyan", "Color name to highlight")
flag.BoolVarP(&o.noTilde, "no-tilde", "t", false, "Not replace home dir path to tilda")
flag.BoolVarP(&o.nonTTY, "non-tty", "y", false, "Bypass the check for non-tty output streams")
flag.BoolVarP(&flagHelp, "help", "h", false, "Show help (This message) and exit")
flag.BoolVarP(&flagVersion, "version", "v", false, "Show version and build command info and exit")
flag.CommandLine.SortFlags = false
flag.Parse()
if flagHelp {
cli.putHelp(fmt.Sprintf("Version %s", getVersion()))
}
if flagVersion {
cli.putErr(versionDetails())
os.Exit(exitOK)
}
return o
}
func versionDetails() string {
goos := runtime.GOOS
goarch := runtime.GOARCH
compiler := runtime.Version()
return fmt.Sprintf(
"Version %s - %s.%s (compiled:%s, %s)",
getVersion(),
goos,
goarch,
compiler,
installFrom,
)
}
func getVersion() string {
if version != "" {
return version
}
i, ok := debug.ReadBuildInfo()
if !ok {
return "Unknown"
}
return i.Main.Version
}
func (cli *runner) putErr(message ...interface{}) {
fmt.Fprintln(cli.err, message...)
}
func (cli *runner) putUsage() {
cli.putErr(fmt.Sprintf("Usage: pwd | %s [OPTIONS]", cmdName))
}
func (cli *runner) putHelp(message string) {
cli.putErr(message)
cli.putUsage()
cli.putErr("Options:")
flag.PrintDefaults()
os.Exit(exitOK)
}