-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathArgs.ahk
301 lines (259 loc) · 7.01 KB
/
Args.ahk
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
Function: Args_Process
processes the command line arguments
Parameters:
byRef command - receives the passed command
byRef subcommand - receives the passed subcommand
byref options - receives an array of passed options. if it's a value option, it's an object
byRef values - receives an array of additional values passed
*/
Args_Process(byRef command, byRef subcommand, byRef options, byRef values)
{
; --------------------------------------------------------------
; parsing arguments into an array
local args := []
Loop %0%
{
args[A_Index] := Trim(%A_Index%, " `t""'")
}
; everything in this method up to here is obsolete in AHK v2
; --------------------------------------------------------------
; initializing variables
options := [], values := []
local finished_option_parsing := false
; loop through arguments
for each, arg in args
{
if (A_Index == 1 && CommandHandler.IsValidCommand(arg))
{
command := arg
continue
}
else if (A_Index == 2 && CommandHandler.IsValidSubcommand(command, arg))
{
; todo: default subcommand
subcommand := arg
continue
}
if (arg == "--")
{
finished_option_parsing := true
continue
}
finished_option_parsing := finished_option_parsing || (!Args_IsOption(args, A_Index) && !Args_IsOptionValue(args, A_Index))
if (!finished_option_parsing) ; parsing options
{
if Args_IsOptionValue(args, A_Index)
continue ; skip, was already included with its option (see 2 lines below)
else if Args_IsValueOption(arg)
options.Insert( { (arg) : args[A_Index + 1] } ) ; include value option and its value in the array
else
options.Insert(arg) ; include simple option in the array
}
else ; parsing values
{
values.Insert(arg)
}
}
}
/*
Function: Args_HasOption()
checks if the command line includes the specified option
Paramters:
args - the argument array as returned by <Args_Parse()>
option - an option string to find, typically a field from the OPT class
Returns:
true if found, false otherwise
*/
Args_HasOption(args, option)
{
return Args_FindOption(args, option) > 0
}
/*
Function: Args_HasOptions()
checks if the command line includes any of the specified options
Parameters:
args - the argument array as returned by <Args_Parse()>
options* - a variadic list of options to check for
Returns:
true if any of the options was found, false if none was found
*/
Args_HasOptions(args, options*)
{
for each, option in options
if Args_HasOption(args, option)
return true
return false
}
/*
Function: Args_HasAllOptions
checks if the command line includes all of the specified options
Parameters:
args - the argument array as returned by <Args_Parse()>
options* - a variadic list of options to check for
Returns:
true if all of the options was found, false if one ore more were not found
*/
Args_HasAllOptions(args, options*)
{
bool := false
for each, option in options
bool := bool && Args_HasOption(args, option)
return bool
}
/*
Function: Args_FindOption()
finds a specified option
Parameters:
args - the argument array as returned by <Args_Parse()>
option - the option string to search for
Returns:
the index of the option in the argument array or -1 if not found
*/
Args_FindOption(args, option)
{
Loop
{
index := Args_FindValue(args, option)
} Until (index == -1 || Args_IsOption(args, index))
return index
}
/*
Function: Args_IsOption()
[for internal use] checks if the specified argument is an option argument or not
Parameters:
args - the argument array as returned by <Args_Parse()>
index - the index of the argument within the array
Returns:
true if it is an option, false otherwise
*/
Args_IsOption(args, index)
{
end := Args_FindValue(args, "--")
return Args_FindValue(OPT, args[index]) > -1 && (end > index || end == -1)
}
Args_IsValueOption(arg)
{
return Args_FindValue(OPT_HANDLER.VALUE_OPTIONS, arg) != -1
}
/*
Function: Args_IsOptionValue()
[for internal use] checks if a specified argument is the value for an option or not
Parameters:
args - the argument array as returned by <Args_Parse()>
index - the index of the argument within the array
Returns:
true if it is a value for an option, false otherwise
*/
Args_IsOptionValue(args, index)
{
return Args_IsOption(args, index - 1) && Args_IsValueOption(args[index - 1])
}
/*
Function: Args_GetOptionValue()
gets the value of a option which has a parameter
Parameters:
args - the argument array as returned by <Args_Parse()>
options* - a variadic list of options to check for
Returns:
the value of the first occurence of one of the specified options
*/
Args_GetOptionValue(args, options*)
{
for each, option in options
{
index := Args_FindOption(args, option)
if (index > 0)
return args[index+1]
}
}
/*
Function: Args_HasOnlyOneOption()
checks if one and only one of the specified options is present
Parameters:
args - the argument array as returned by <Args_Parse()>
options* - a variadic list of options to check for
Returns:
false if 0 or > 1 occurrences of any of the options were found, true if there's just 1
*/
Args_HasOnlyOneOption(args, options*)
{
found := false, temp_found := false
for each, option in options
{
temp_found := Args_HasOption(args, option)
if (found)
return false
found := temp_found
}
return found
}
; ===========================================================================================================
/*
Function: Args_GetValueParam()
gets the value of a parameter which is *not an option*
Parameters:
args - the argument array as returned by <Args_Parse()>
index - the index of the value parameter ("search for the %index%st value param")
Returns:
the value of that parameter
*/
Args_GetValueParam(args, index)
{
return args[Args_FindValueParam(args, index)]
}
/*
Function: Args_CountValueParams()
counts the number of parameters that are not options
Parameters:
args - the argument array as returned by <Args_Parse()>
Returns:
the count (0 if none were specified)
*/
Args_CountValueParams(args)
{
count := 0
while (Args_FindValueParam(args, A_Index) > -1)
count++
return count
}
/*
Function: Args_FindValueParam()
[for internal use] finds a parameter which is not an option
Parameters:
args - the argument array as returned by <Args_Parse()>
index - the index of the value parameter ("search for the %index%st value param")
Returns:
the index of the parameter, if found. -1 if none is found.
*/
Args_FindValueParam(args, index)
{
foundIndex := 0
for each, arg in args
{
if (A_Index == 1 || A_Index < index)
continue
if (!Args_IsOption(args, A_Index) && !Args_IsOptionValue(args, A_Index) && arg != "--")
foundIndex++
if (foundIndex == index)
return A_Index
}
return -1
}
; ===========================================================================================================
/*
Function: Args_FindValue
[for internal use] finds a value in the argument array
Parameters:
args - the argument array as returned by <Args_Parse()>
value - the value to find
Returns:
the index if found, -1 otherwise
*/
Args_FindValue(args, value)
{
for each, arg in args
if (arg == value)
return A_Index
return -1
}