-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
78 lines (65 loc) · 1.39 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
package main
import (
"flag"
"fmt"
"os"
"strings"
)
var (
openEditorFlag bool
printConfigFlag bool
profileNameFlag string
)
func extractCliVars() ([]string, map[string]string) {
cliArgs := make([]string, 0)
cliVars := make(map[string]string)
for _, arg := range flag.Args() {
if !strings.Contains(arg, "=") {
cliArgs = append(cliArgs, arg)
continue
}
pair := strings.SplitN(arg, "=", 2)
key, val := pair[0], pair[1]
if strings.HasPrefix(val, "\"") && strings.HasSuffix(val, "\"") {
val = val[1 : len(val)-1]
} else if strings.HasPrefix(val, "'") && strings.HasSuffix(val, "'") {
val = val[1 : len(val)-1]
}
cliVars[key] = val
}
return cliArgs, cliVars
}
func parseFlags() {
flag.BoolVar(
&printConfigFlag,
"c",
false,
"print the default configuration to STDOUT",
)
flag.BoolVar(
&openEditorFlag,
"e",
false,
"open the editor after writing the file",
)
flag.StringVar(
&profileNameFlag,
"p",
"default",
"name of the profile to use",
)
flag.Usage = func() {
fmt.Fprintf(
os.Stderr,
`USAGE: %s [-c] [-e] [-p PROFILE] INPUT
INPUT must be a list of arguments or - to get the input from the STDIN.
The input can also contains arguments using key=value format to pass variables for the template.
If no input is given, the output file will be opened in a text editor.
OPTIONS:
`,
APP_NAME,
)
flag.PrintDefaults()
}
flag.Parse()
}