-
Notifications
You must be signed in to change notification settings - Fork 8
/
docopts.go
148 lines (122 loc) · 2.77 KB
/
docopts.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package docopt
import (
"fmt"
"strconv"
"strings"
dopt "github.com/docopt/docopt-go"
)
// aliases for docopt types.
type (
Opts = dopt.Opts
Option = dopt.Option
)
var docoptParser = &dopt.Parser{
HelpHandler: dopt.NoHelpHandler,
OptionsFirst: false,
SkipHelpFlags: false,
}
// Parse parses docopts for command options with args from os.Args.
func Parse(cmdName string, args []string, docopts string) (Opts, error) {
// no options at all
if docopts == "" {
return Opts{}, nil
}
return docoptParser.ParseArgs(docopts, append([]string{cmdName}, args...), "")
}
// ParseOptions parses docopts only to get all available options for a command.
func ParseOptions(docopts string, cmdName string) ([]Option, error) {
return docoptParser.ParseOptions(docopts, []string{cmdName})
}
func OptsToLetsOpt(opts Opts) map[string]string {
envMap := make(map[string]string, len(opts))
for origKey, value := range opts {
if !isOptKey(origKey) {
continue
}
key := normalizeKey(origKey)
envKey := fmt.Sprintf("LETSOPT_%s", key)
var strValue string
switch value := value.(type) {
case string:
strValue = value
case bool:
if value {
strValue = strconv.FormatBool(value)
} else {
strValue = ""
}
case []string:
strValue = strings.Join(value, " ")
case nil:
strValue = ""
default:
strValue = ""
}
envMap[envKey] = strValue
}
return envMap
}
func OptsToLetsCli(opts Opts) map[string]string {
cliMap := make(map[string]string, len(opts))
formatVal := func(k, v string) string {
return fmt.Sprintf("%s %s", k, v)
}
for origKey, value := range opts {
if !isOptKey(origKey) {
continue
}
key := normalizeKey(origKey)
cliKey := fmt.Sprintf("LETSCLI_%s", key)
var strValue string
switch value := value.(type) {
case string:
if value != "" {
strValue = formatVal(origKey, value)
}
case bool:
if value {
strValue = origKey
}
case []string:
if len(value) == 0 {
strValue = ""
} else {
values := value
if strings.HasPrefix(origKey, "-") {
values = append([]string{origKey}, values...)
}
// TODO maybe we should escape each value
strValue = strings.Join(values, " ")
}
case nil:
strValue = ""
default:
strValue = ""
}
cliMap[cliKey] = strValue
}
return cliMap
}
func isOptKey(key string) bool {
if key == "--" {
return false
}
if strings.HasPrefix(key, "--") {
return true
}
if strings.HasPrefix(key, "-") {
return true
}
if strings.HasPrefix(key, "<") && strings.HasSuffix(key, ">") {
return true
}
return false
}
func normalizeKey(origKey string) string {
key := strings.TrimLeft(origKey, "-")
key = strings.TrimLeft(key, "<")
key = strings.TrimRight(key, ">")
key = strings.ReplaceAll(key, "-", "_")
key = strings.ToUpper(key)
return key
}