forked from fenekku/commandeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandeer.nim
278 lines (230 loc) · 7.84 KB
/
commandeer.nim
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import parseopt2
import strutils
import strtabs
var
argumentList = newSeq[string]()
shortOptions = newStringTable(modeCaseSensitive)
longOptions = newStringTable(modeCaseSensitive)
argumentIndex = 0
errorMsgs : seq[string] = @[]
customErrorMsg : string
inSubcommand = false
subcommandSelected = false
## String conversion
proc convert(s : string, t : char): char =
result = s[0]
proc convert(s : string, t : int): int =
result = parseInt(s)
proc convert(s : string, t : float): float =
result = parseFloat(s)
proc convert(s : string, t : bool): bool =
## will accept "yes", "true" as true values
if s == "":
## the only way we get an empty string here is because of a key
## with no value, in which case the presence of the key is enough
## to return true
result = true
else:
result = parseBool(s)
proc convert(s : string, t : string): string =
result = s.strip
template argumentIMPL(identifier : expr, t : typeDesc): stmt {.immediate.} =
bind argumentList
bind argumentIndex
bind convert
bind errorMsgs
bind inSubcommand
bind subcommandSelected
var identifier : t
if (inSubcommand and subcommandSelected) or not inSubcommand:
if len(argumentList) <= argumentIndex:
let eMsg = "Missing command line arguments"
if len(errorMsgs) == 0:
errorMsgs.add(eMsg)
else:
if not (errorMsgs[high(errorMsgs)][0] == 'M'):
errorMsgs.add(eMsg)
else:
var typeVar : t
try:
identifier = convert(argumentList[argumentIndex], typeVar)
except ValueError:
let eMsg = capitalize(getCurrentExceptionMsg()) &
" for argument " & $(argumentIndex+1)
errorMsgs.add(eMsg)
inc(argumentIndex)
template argumentsIMPL(identifier : expr, t : typeDesc, atLeast1 : bool): stmt {.immediate.} =
bind argumentList
bind argumentIndex
bind convert
bind errorMsgs
bind inSubcommand
bind subcommandSelected
var identifier = newSeq[t]()
if (inSubcommand and subcommandSelected) or not inSubcommand:
if len(argumentList) <= argumentIndex:
if atLeast1:
let eMsg = "Missing command line arguments"
if len(errorMsgs) == 0:
errorMsgs.add(eMsg)
else:
if not (errorMsgs[high(errorMsgs)][0] == 'M'):
errorMsgs.add(eMsg)
else:
discard
else:
var typeVar : t
var firstError = true
while true:
if len(argumentList) == argumentIndex:
break
try:
let argument = argumentList[argumentIndex]
inc(argumentIndex)
identifier.add(convert(argument, typeVar))
firstError = false
except ValueError:
if atLeast1 and firstError:
let eMsg = capitalize(getCurrentExceptionMsg()) &
" for argument " & $(argumentIndex+1)
errorMsgs.add(eMsg)
break
template optionDefaultIMPL(identifier : expr, t : typeDesc, longName : string,
shortName : string, default : t): stmt {.immediate.} =
bind shortOptions
bind longOptions
bind convert
bind errorMsgs
bind strtabs
bind inSubcommand
bind subcommandSelected
var identifier : t
if (inSubcommand and subcommandSelected) or not inSubcommand:
var typeVar : t
if strtabs.hasKey(longOptions, longName):
try:
identifier = convert(longOptions[longName], typeVar)
except ValueError:
let eMsg = capitalize(getCurrentExceptionMsg()) &
" for option --" & longName
errorMsgs.add(eMsg)
elif strtabs.hasKey(shortOptions, shortName):
try:
identifier = convert(shortOptions[shortName], typeVar)
except ValueError:
let eMsg = capitalize(getCurrentExceptionMsg()) &
" for option -" & shortName
errorMsgs.add(eMsg)
else:
#default values
identifier = default
template optionIMPL(identifier : expr, t : typeDesc, longName : string,
shortName : string): stmt {.immediate.} =
bind shortOptions
bind longOptions
bind convert
bind errorMsgs
bind strtabs
bind inSubcommand
bind subcommandSelected
var identifier : t
if (inSubcommand and subcommandSelected) or not inSubcommand:
var typeVar : t
if strtabs.hasKey(longOptions, longName):
try:
identifier = convert(longOptions[longName], typeVar)
except ValueError:
let eMsg = capitalize(getCurrentExceptionMsg()) &
" for option --" & longName
errorMsgs.add(eMsg)
elif strtabs.hasKey(shortOptions, shortName):
try:
identifier = convert(shortOptions[shortName], typeVar)
except ValueError:
let eMsg = capitalize(getCurrentExceptionMsg()) &
" for option -" & shortName
errorMsgs.add(eMsg)
template exitoptionIMPL(longName, shortName, msg : string): stmt =
bind shortOptions
bind longOptions
bind strtabs
if (inSubcommand and subcommandSelected) or not inSubcommand:
if strtabs.hasKey(longOptions, longName):
quit msg, QuitSuccess
elif strtabs.hasKey(shortOptions, shortName):
quit msg, QuitSuccess
template errormsgIMPL(msg : string): stmt =
bind customErrorMsg
if (inSubcommand and subcommandSelected) or not inSubcommand:
customErrorMsg = msg
template subcommandIMPL(identifier : expr, subcommandName : string, stmts : stmt): stmt {.immediate.} =
bind argumentList
bind argumentIndex
bind errorMsgs
bind inSubcommand
bind subcommandSelected
var identifier : bool = false
inSubcommand = true
if len(argumentList) > 0 and argumentList[0] == subcommandName:
identifier = true
inc(argumentIndex)
subcommandSelected = true
stmts
subcommandSelected = false
inSubcommand = false
template commandline*(statements : stmt): stmt {.immediate.} =
bind argumentList
bind shortOptions
bind longOptions
bind errorMsgs
bind parseopt2
bind strtabs
bind customErrorMsg
template argument(identifier : expr, t : typeDesc): stmt {.immediate.} =
argumentIMPL(identifier, t)
template arguments(identifier : expr, t : typeDesc, atLeast1 : bool = true): stmt {.immediate.} =
argumentsIMPL(identifier, t, atLeast1)
template option(identifier : expr, t : typeDesc, longName : string,
shortName : string, default : expr): stmt =
optionDefaultIMPL(identifier, t, longName, shortName, default)
template option(identifier : expr, t : typeDesc, longName : string,
shortName : string): stmt =
optionIMPL(identifier, t, longName, shortName)
template exitoption(longName, shortName, msg : string): stmt =
exitoptionIMPL(longName, shortName, msg)
template errormsg(msg : string): stmt =
errormsgIMPL(msg)
template subcommand(identifier : expr, subcommandName : string, stmts : stmt): stmt {.immediate.} =
subcommandIMPL(identifier, subcommandName, stmts)
for kind, key, value in parseopt2.getopt():
case kind
of parseopt2.cmdArgument:
argumentList.add(key)
of parseopt2.cmdLongOption:
longOptions[key] = value
of parseopt2.cmdShortOption:
shortOptions[key] = value
# strtabs.add(shortOptions, key, value)
else:
discard
#Call the passed statements so that the above templates are called
statements
if len(errorMsgs) > 0:
if not customErrorMsg.isNil:
errorMsgs.add(customErrorMsg)
quit join(errorMsgs, "\n")
when isMainModule:
import unittest
test "convert() returns converted type value from strings":
var intVar : int
var floatVar : float
var boolVar : bool
var stringVar : string
var charVar : char
check convert("10", intVar) == 10
check convert("10.0", floatVar) == 10
check convert("10", floatVar) == 10
check convert("yes", boolVar) == true
check convert("false", boolVar) == false
check convert("no ", stringVar) == "no"
check convert("*", charVar) == '*'