-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
177 lines (138 loc) · 3.44 KB
/
parse.go
File metadata and controls
177 lines (138 loc) · 3.44 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package commando
import (
"fmt"
"strconv"
"strings"
"github.com/wh64dev/commando/option"
)
func convertFullOption(opt option.OptionData, args []string) {
for i, arg := range args {
if strings.HasPrefix(arg, "--") {
continue
}
if !strings.HasPrefix(arg, "-") {
continue
}
for _, alias := range opt.Alias {
if !strings.Contains(arg, alias) {
continue
}
args[i] = fmt.Sprintf("--%s", opt.Name)
break
}
}
}
func untagPrefix(arg string) string {
var value = arg
value = strings.TrimPrefix(value, "--")
value = strings.TrimPrefix(value, "-")
return value
}
func extractIndex(name string, args []string) (int, error) {
for i, arg := range args {
var ret = arg
if !strings.HasPrefix(ret, "-") && !strings.HasPrefix(ret, "--") {
continue
}
ret = untagPrefix(arg)
if ret != name {
continue
}
return i, nil
}
return -1, fmt.Errorf("option --%s is not defined", name)
}
func ParseString(n *CommandoNode, opt option.OptionData) (string, error) {
if opt.Type != option.STRING {
return "", fmt.Errorf("option --%s is not a string", opt.Name)
}
var args = n.Commando.args
if opt.Alias != nil {
convertFullOption(opt, args)
}
var index, err = extractIndex(opt.Name, args)
if err != nil {
return "", err
}
if index == -1 {
return "", nil
}
if index+1 >= len(args) {
return "", fmt.Errorf("option --%s value is not defined", opt.Name)
}
return n.Commando.args[index+1], nil
}
func ParseInt(n *CommandoNode, opt option.OptionData) (int64, error) {
if opt.Type != option.INTEGER {
return 0, fmt.Errorf("option --%s is not a integer", opt.Name)
}
var args = n.Commando.args
if opt.Alias != nil {
convertFullOption(opt, args)
}
var index, err = extractIndex(opt.Name, args)
if err != nil {
return 0, err
}
if index == -1 {
return 0, nil
}
if index+1 >= len(args) {
return 0, fmt.Errorf("option --%s value is not defined", opt.Name)
}
var arg = n.Commando.args[index+1]
if strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") {
return 0, fmt.Errorf("option --%s value is not defined", opt.Name)
}
return strconv.ParseInt(arg, 0, 64)
}
func ParseFloat(n *CommandoNode, opt option.OptionData) (float64, error) {
if opt.Type != option.FLOAT {
return 0, fmt.Errorf("--%s is not a string", opt.Name)
}
var args = n.Commando.args
if opt.Alias != nil {
convertFullOption(opt, args)
}
index, err := extractIndex(opt.Name, n.Commando.args)
if err != nil {
return 0, err
}
if index == -1 {
return 0, nil
}
if index+1 >= len(n.Commando.args) {
return 0, fmt.Errorf("option --%s value is not defined", opt.Name)
}
arg := n.Commando.args[index+1]
if strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") {
return 0, fmt.Errorf("option --%s value is not defined", opt.Name)
}
return strconv.ParseFloat(arg, 64)
}
func ParseBool(n *CommandoNode, opt option.OptionData) (bool, error) {
if opt.Type != option.BOOLEAN {
return false, fmt.Errorf("option --%s is not a bool", opt.Name)
}
var args = n.Commando.args
if opt.Alias != nil {
convertFullOption(opt, args)
}
index, err := extractIndex(opt.Name, n.Commando.args)
if err != nil {
return false, nil
}
if index+1 >= len(n.Commando.args) {
return true, nil
}
var arg = n.Commando.args[index+1]
if arg != "true" && arg != "false" && arg != "1" && arg != "0" {
return true, nil
}
var parsed bool
parsed, err = strconv.ParseBool(arg)
if err != nil {
return false, err
}
return parsed, nil
}