ruby-getoptions 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -7
- data/lib/ruby-getoptions.rb +243 -162
- metadata +28 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
---
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
---
|
2
|
+
SHA512:
|
3
|
+
data.tar.gz: 4e87fd83b217eef7cca4fffba965c41c76e40b805ddd09febde6f808d655f1718711061d71448e57fd93bb61b5774dd79df7ea75e3923ac1578eaebbe16514a6
|
4
|
+
metadata.gz: 7a2206f26c03d4ba32b298bb0ee679b23796d6918d42fb144ab15b3863bb9c86f1a011c994e40e302cde3484b99205c0a8c8c427c27f62d9063453469c76bf92
|
5
|
+
SHA1:
|
6
|
+
data.tar.gz: e297aaaa3b5ef68a1edd94351412c755f8ce478c
|
7
|
+
metadata.gz: d810f9c4002a8a5503904926d72fae813804b5b0
|
data/lib/ruby-getoptions.rb
CHANGED
@@ -37,32 +37,43 @@ class GetOptions
|
|
37
37
|
# External method, this is the main interface
|
38
38
|
def self.parse(args, option_map = {}, options = {})
|
39
39
|
@options = options
|
40
|
-
|
40
|
+
@option_map = {}
|
41
|
+
@level = 2
|
41
42
|
set_logging()
|
42
43
|
info "input args: '#{args}'"
|
43
44
|
info "input option_map: '#{option_map}'"
|
44
45
|
info "input options: '#{options}'"
|
45
46
|
@option_map = generate_extended_option_map(option_map)
|
46
|
-
option_result, remaining_args =
|
47
|
+
option_result, remaining_args = iterate_over_arguments(args, options[:mode])
|
47
48
|
debug "option_result: '#{option_result}', remaining_args: '#{remaining_args}'"
|
48
49
|
@log = nil
|
49
50
|
[option_result, remaining_args]
|
50
51
|
end
|
51
52
|
|
52
53
|
private
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
54
|
+
|
55
|
+
# Regex definitions
|
56
|
+
ALIASES_REGEX = /^([^=:+!]+)([=:+!]?.*?)$/
|
57
|
+
IS_OPTION_REGEX = /^--?[^\d]/
|
58
|
+
INTEGER_REGEX = /\A[+-]?\d+?\Z/
|
59
|
+
NUMERIC_REGEX = /\A[+-]?\d+?(\.\d+)?([eE]\d+)?\Z/
|
60
|
+
OPT_SPEC_REGEX = /^([=:])([siof])([@%]?)((?:\{[^}]+\})?)$/
|
61
|
+
NFLAG_REGEX = /^no-?/
|
62
|
+
KEY_VALUE_REGEX = /^([^=]+)=(.*)$/
|
63
|
+
OPTION_REGEX = /^(--?)([^=]+)(=?)(.*?)$/
|
64
|
+
REPEAT_REGEX = /\{(\d+)?(?:,\s?(\d+)?)?\}/
|
65
|
+
NO_DEFINITION_REGEX = /^[=:+!]/
|
66
|
+
|
67
|
+
# This is how the instance variable @option_map looks like:
|
68
|
+
# @option_map:
|
69
|
+
# {
|
70
|
+
# ["opt", "alias"] => {
|
71
|
+
# :arg_spec=>"nflag",
|
72
|
+
# :arg_opts=>["b", nil, nil],
|
73
|
+
# :opt_dest=>:flag3,
|
74
|
+
# :negated=> true
|
75
|
+
# }
|
76
|
+
# }
|
66
77
|
|
67
78
|
def self.info(msg)
|
68
79
|
STDERR.puts "INFO |" + msg if @level <= 1
|
@@ -85,29 +96,54 @@ private
|
|
85
96
|
end
|
86
97
|
end
|
87
98
|
|
99
|
+
# Given an option definition, it extracts the aliases and puts them into an array.
|
100
|
+
#
|
101
|
+
# @: definition
|
102
|
+
# return: [aliases, ...]
|
103
|
+
def self.extract_spec_and_aliases(definition)
|
104
|
+
m = ALIASES_REGEX.match(definition)
|
105
|
+
return m[2], m[1].split('|')
|
106
|
+
end
|
107
|
+
|
88
108
|
def self.generate_extended_option_map(option_map)
|
89
109
|
opt_map = {}
|
90
|
-
|
110
|
+
definition_list = []
|
91
111
|
option_map.each_pair do |k, v|
|
92
|
-
if k
|
112
|
+
if NO_DEFINITION_REGEX =~ k
|
93
113
|
fail ArgumentError,
|
94
114
|
"GetOptions option_map missing name in definition: '#{k}'"
|
95
115
|
end
|
96
|
-
definitions = k
|
97
|
-
|
98
|
-
arg_spec, *arg_opts = process_type(k.match(/^[^#{@valid_simbols}]+([#{@valid_simbols}]?(.*?))$/)[1])
|
116
|
+
opt_spec, definitions = extract_spec_and_aliases(k)
|
117
|
+
arg_spec, *arg_opts = process_opt_spec(opt_spec)
|
99
118
|
opt_map[definitions] = { :arg_spec => arg_spec, :arg_opts => arg_opts, :opt_dest => v }
|
119
|
+
|
120
|
+
definition_list.push(*definitions)
|
100
121
|
end
|
101
|
-
|
102
|
-
duplicate_elements = unique_options.find { |e| unique_options.count(e) > 1 }
|
103
|
-
fail ArgumentError,
|
104
|
-
"GetOptions option_map needs to have unique options: '#{duplicate_elements}'"
|
105
|
-
end
|
122
|
+
fail_on_duplicate_definitions(definition_list)
|
106
123
|
debug "opt_map: #{opt_map}"
|
107
124
|
opt_map
|
108
125
|
end
|
109
126
|
|
110
|
-
def self.
|
127
|
+
def self.fail_on_duplicate_definitions(definition_list)
|
128
|
+
definition_list.map!{ |x| x.downcase }
|
129
|
+
unless definition_list.uniq.length == definition_list.length
|
130
|
+
duplicate_elements = definition_list.find { |e| definition_list.count(e) > 1 }
|
131
|
+
fail ArgumentError,
|
132
|
+
"GetOptions option_map needs to have unique case insensitive options: '#{duplicate_elements}'"
|
133
|
+
end
|
134
|
+
true
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
# Checks an option specification string and returns an array with
|
139
|
+
# argument_spec, type, destype and repeat.
|
140
|
+
#
|
141
|
+
# The Option Specification provides a nice, compact interface. This method
|
142
|
+
# extracts the different parts from that.
|
143
|
+
#
|
144
|
+
# @: type definition
|
145
|
+
# return arg_spec, type, destype, repeat
|
146
|
+
def self.process_opt_spec(opt_spec)
|
111
147
|
# argument_specification:
|
112
148
|
# [ '',
|
113
149
|
# '!',
|
@@ -121,102 +157,122 @@ private
|
|
121
157
|
# destype: ['@', '%']
|
122
158
|
# repeat: { [ min ] [ , [ max ] ] }
|
123
159
|
|
124
|
-
#
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
160
|
+
# Handle special cases
|
161
|
+
case opt_spec
|
162
|
+
when ''
|
163
|
+
return 'flag', 'b', nil, nil
|
164
|
+
when '!'
|
165
|
+
return 'nflag', 'b', nil, nil
|
166
|
+
when '+'
|
167
|
+
return 'increment', 'i', nil, nil
|
168
|
+
end
|
169
|
+
|
170
|
+
arg_spec = String.new
|
171
|
+
type = nil
|
172
|
+
desttype = nil
|
173
|
+
repeat = nil
|
174
|
+
|
175
|
+
matches = OPT_SPEC_REGEX.match(opt_spec)
|
176
|
+
if matches.nil?
|
177
|
+
fail ArgumentError, "Wrong option specification: '#{opt_spec}'!"
|
178
|
+
end
|
179
|
+
case matches[1]
|
180
|
+
when '='
|
181
|
+
arg_spec = 'required'
|
182
|
+
when ':'
|
183
|
+
arg_spec = 'optional'
|
184
|
+
end
|
185
|
+
type = matches[2]
|
186
|
+
if matches[3] != ''
|
187
|
+
desttype = matches[3]
|
188
|
+
end
|
189
|
+
if matches[4] != ''
|
190
|
+
r_matches = REPEAT_REGEX.match(matches[4])
|
191
|
+
min = r_matches[1]
|
192
|
+
min ||= 1
|
193
|
+
min = min.to_i
|
194
|
+
max = r_matches[2]
|
195
|
+
max = min if max.nil?
|
196
|
+
max = max.to_i
|
197
|
+
if min > max
|
198
|
+
fail ArgumentError, "GetOptions repeat, max '#{max}' <= min '#{min}'"
|
199
|
+
end
|
200
|
+
repeat = [min, max]
|
147
201
|
end
|
202
|
+
return arg_spec, type, desttype, repeat
|
148
203
|
end
|
149
204
|
|
150
|
-
def self.
|
151
|
-
|
205
|
+
def self.iterate_over_arguments(args, mode)
|
206
|
+
option_result = {}
|
207
|
+
remaining_args = []
|
208
|
+
while args.size > 0
|
152
209
|
arg = args.shift
|
153
|
-
|
210
|
+
options, argument = isOption?(arg, mode)
|
211
|
+
if options.size >= 1 && options[0] == '--'
|
154
212
|
remaining_args.push(*args)
|
155
213
|
return option_result, remaining_args
|
156
|
-
elsif
|
157
|
-
option_result,
|
158
|
-
option_result, remaining_args = process_arguments(args, option_result, remaining_args)
|
214
|
+
elsif options.size >= 1
|
215
|
+
option_result, remaining_args, args = process_option(arg, option_result, args, remaining_args, options, argument)
|
159
216
|
else
|
160
217
|
remaining_args.push arg
|
161
|
-
option_result, remaining_args = process_arguments(args, option_result, remaining_args)
|
162
218
|
end
|
163
219
|
end
|
164
220
|
return option_result, remaining_args
|
165
221
|
end
|
166
222
|
|
167
|
-
def self.process_option(orig_opt, option_result, args, remaining_args)
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
223
|
+
def self.process_option(orig_opt, option_result, args, remaining_args, options, argument)
|
224
|
+
options.each_with_index do |opt, i|
|
225
|
+
# Make it obvious that find_option_matches is updating the instance variable
|
226
|
+
opt_match, @option_map = find_option_matches(options[i])
|
227
|
+
if opt_match.nil?
|
228
|
+
remaining_args.push orig_opt
|
229
|
+
return option_result, remaining_args, args
|
230
|
+
end
|
231
|
+
# Only pass argument to the last option in the options array
|
232
|
+
args.unshift argument unless argument.nil? || argument == "" || i < (options.size - 1)
|
233
|
+
debug "new args: #{args}"
|
234
|
+
option_result, args = execute_option(opt_match, option_result, args)
|
235
|
+
debug "option_result: #{option_result}"
|
179
236
|
end
|
180
|
-
|
181
|
-
debug "new args: #{args}"
|
182
|
-
option_result, args = execute_option(opt_match, option_result, args)
|
183
|
-
debug "option_result: #{option_result}"
|
184
|
-
return option_result, args, remaining_args
|
237
|
+
return option_result, remaining_args, args
|
185
238
|
end
|
186
239
|
|
187
|
-
|
240
|
+
# find_option_matches_in_hash iterates over the option_map hash and returns
|
241
|
+
# a list of entries that match the given option.
|
242
|
+
#
|
243
|
+
# NOTE: This method updates the given hash.
|
244
|
+
#
|
245
|
+
# @: option, hash, regex
|
246
|
+
# return: matches, hash
|
247
|
+
def self.find_option_matches_in_hash(opt, hash, regex)
|
188
248
|
matches = []
|
189
|
-
|
249
|
+
hash.each_pair do |k, v|
|
190
250
|
local_matches = []
|
191
|
-
k.map { |name| local_matches.push name if
|
251
|
+
k.map { |name| local_matches.push name if regex.match(name) }
|
192
252
|
if v[:arg_spec] == 'nflag'
|
193
253
|
k.map do |name|
|
194
|
-
if opt
|
195
|
-
# Update the
|
196
|
-
|
254
|
+
if NFLAG_REGEX =~ opt && /^#{opt.gsub(/no-?/, '')}$/ =~ name
|
255
|
+
# Update the given hash
|
256
|
+
hash[k][:negated] = true
|
197
257
|
local_matches.push name
|
258
|
+
debug "hash: #{hash}"
|
198
259
|
end
|
199
260
|
end
|
200
261
|
end
|
201
262
|
matches.push(k) if local_matches.size > 0
|
202
263
|
end
|
203
|
-
|
264
|
+
return matches, hash
|
265
|
+
end
|
266
|
+
|
267
|
+
def self.find_option_matches(opt)
|
268
|
+
matches = []
|
269
|
+
m, @option_map = find_option_matches_in_hash(opt, @option_map, /^#{opt}$/)
|
270
|
+
matches.push(*m)
|
271
|
+
|
204
272
|
# If the strict match returns no results, lets be more permisive.
|
205
273
|
if matches.size == 0
|
206
|
-
@option_map
|
207
|
-
|
208
|
-
k.map { |name| local_matches.push name if name.match(/^#{opt}/) }
|
209
|
-
if v[:arg_spec] == 'nflag'
|
210
|
-
k.map do |name|
|
211
|
-
if opt.match(/^no-?/) && name.match(/^#{opt.gsub(/^no-?/, '')}/)
|
212
|
-
# Update the instance variable
|
213
|
-
@option_map[k][:negated] = true
|
214
|
-
local_matches.push name
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
matches.push(k) if local_matches.size > 0
|
219
|
-
end
|
274
|
+
m, @option_map = find_option_matches_in_hash(opt, @option_map, /^#{opt}/)
|
275
|
+
matches.push(*m)
|
220
276
|
end
|
221
277
|
|
222
278
|
if matches.size == 0
|
@@ -228,12 +284,14 @@ private
|
|
228
284
|
return [nil, @option_map]
|
229
285
|
end
|
230
286
|
elsif matches.size > 1
|
231
|
-
abort "[ERROR] option '#{opt}' matches multiple names '#{matches.inspect}'!"
|
287
|
+
abort "[ERROR] option '#{opt}' matches multiple names '#{matches.sort.inspect}'!"
|
232
288
|
end
|
233
289
|
debug "matches: #{matches}"
|
234
290
|
[matches[0], @option_map]
|
235
291
|
end
|
236
292
|
|
293
|
+
# TODO: Some specs allow for Symbols and procedures, others only Symbols.
|
294
|
+
# Fail during init and not during run time.
|
237
295
|
def self.execute_option(opt_match, option_result, args)
|
238
296
|
opt_def = @option_map[opt_match]
|
239
297
|
debug "#{opt_def[:arg_spec]}"
|
@@ -268,103 +326,90 @@ private
|
|
268
326
|
[option_result, args]
|
269
327
|
end
|
270
328
|
|
329
|
+
# process_option_type Given an arg, it checks what type is the option expecting and based on that saves
|
271
330
|
def self.process_option_type(arg, opt_match, optional = false)
|
272
331
|
case @option_map[opt_match][:arg_opts][0]
|
273
332
|
when 's'
|
274
333
|
arg = '' if optional && arg.nil?
|
275
334
|
when 'i'
|
276
335
|
arg = 0 if optional && arg.nil?
|
277
|
-
|
278
|
-
abort "[ERROR] argument for option '#{opt_match[0]}' is not of type 'Integer'!"
|
279
|
-
end
|
336
|
+
type_error(arg, opt_match[0], 'Integer', lambda { |x| integer?(x) })
|
280
337
|
arg = arg.to_i
|
281
338
|
when 'f'
|
282
339
|
arg = 0 if optional && arg.nil?
|
283
|
-
|
284
|
-
abort "[ERROR] argument for option '#{opt_match[0]}' is not of type 'Float'!"
|
285
|
-
end
|
340
|
+
type_error(arg, opt_match[0], 'Float', lambda { |x| numeric?(x) })
|
286
341
|
arg = arg.to_f
|
287
342
|
when 'o'
|
288
|
-
#
|
343
|
+
# TODO
|
289
344
|
abort "[ERROR] Unimplemented type 'o'!"
|
290
345
|
end
|
291
346
|
return arg
|
292
347
|
end
|
293
348
|
|
349
|
+
def self.type_error(arg, opt, type, func)
|
350
|
+
unless func.call(arg)
|
351
|
+
abort "[ERROR] argument for option '#{opt}' is not of type '#{type}'!"
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
294
355
|
def self.process_desttype(option_result, args, opt_match, optional = false)
|
295
356
|
opt_def = @option_map[opt_match]
|
296
357
|
case opt_def[:arg_opts][1]
|
297
358
|
when '@'
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
359
|
+
check_for_repeat(Array, option_result, args, opt_match, optional, opt_def)
|
360
|
+
when '%'
|
361
|
+
check_for_repeat(Hash, option_result, args, opt_match, optional, opt_def)
|
362
|
+
else
|
363
|
+
args, arg = process_desttype_arg(args, opt_match, optional)
|
364
|
+
option_result[opt_def[:opt_dest]] = arg
|
365
|
+
end
|
366
|
+
[option_result, args]
|
367
|
+
end
|
368
|
+
|
369
|
+
def self.check_for_repeat(type, option_result, args, opt_match, optional, opt_def)
|
370
|
+
unless option_result[opt_def[:opt_dest]].kind_of? type
|
371
|
+
option_result[opt_def[:opt_dest]] = type.new
|
372
|
+
end
|
373
|
+
# check for repeat
|
374
|
+
if !opt_def[:arg_opts][2].nil?
|
375
|
+
min = opt_def[:arg_opts][2][0]
|
376
|
+
max = opt_def[:arg_opts][2][1]
|
377
|
+
while min > 0
|
378
|
+
debug "min: #{min}, max: #{max}"
|
379
|
+
min -= 1
|
380
|
+
max -= 1
|
381
|
+
abort "[ERROR] missing argument for option '#{opt_match[0]}'!" if args.size <= 0
|
382
|
+
if type == Array
|
315
383
|
args, arg = process_desttype_arg(args, opt_match, optional)
|
316
384
|
option_result[opt_def[:opt_dest]].push arg
|
385
|
+
elsif type == Hash
|
386
|
+
args, arg, key = process_desttype_hash_arg(args, opt_match, optional)
|
387
|
+
option_result[opt_def[:opt_dest]][key] = arg
|
317
388
|
end
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
389
|
+
end
|
390
|
+
while max > 0
|
391
|
+
debug "min: #{min}, max: #{max}"
|
392
|
+
max -= 1
|
393
|
+
break if args.size <= 0
|
394
|
+
if type == Array
|
322
395
|
args, arg = process_desttype_arg(args, opt_match, optional, true)
|
323
396
|
break if arg.nil?
|
324
397
|
option_result[opt_def[:opt_dest]].push arg
|
325
|
-
|
326
|
-
else
|
327
|
-
args, arg = process_desttype_arg(args, opt_match, optional)
|
328
|
-
option_result[opt_def[:opt_dest]].push arg
|
329
|
-
end
|
330
|
-
when '%'
|
331
|
-
unless option_result[opt_def[:opt_dest]].kind_of? Hash
|
332
|
-
option_result[opt_def[:opt_dest]] = {}
|
333
|
-
end
|
334
|
-
# check for repeat specifier {min, max}
|
335
|
-
if (matches = opt_def[:arg_opts][2].match(/\{(\d+)(?:,\s?(\d+))?\}/))
|
336
|
-
min = matches[1].to_i
|
337
|
-
max = matches[2]
|
338
|
-
max = min if max.nil?
|
339
|
-
max = max.to_i
|
340
|
-
if min > max
|
341
|
-
fail ArgumentError, "GetOptions repeat, max '#{max}' <= min '#{min}'"
|
342
|
-
end
|
343
|
-
while min > 0
|
344
|
-
debug "min: #{min}, max: #{max}"
|
345
|
-
min -= 1
|
346
|
-
max -= 1
|
347
|
-
abort "[ERROR] missing argument for option '#{opt_match[0]}'!" if args.size <= 0
|
348
|
-
args, arg, key = process_desttype_hash_arg(args, opt_match, optional)
|
349
|
-
option_result[opt_def[:opt_dest]][key] = arg
|
350
|
-
end
|
351
|
-
while max > 0
|
352
|
-
debug "min: #{min}, max: #{max}"
|
353
|
-
max -= 1
|
354
|
-
break if args.size <= 0
|
398
|
+
elsif type == Hash
|
355
399
|
break if option?(args[0])
|
356
400
|
args, arg, key = process_desttype_hash_arg(args, opt_match, optional)
|
357
401
|
option_result[opt_def[:opt_dest]][key] = arg
|
358
402
|
end
|
359
|
-
|
403
|
+
end
|
404
|
+
else
|
405
|
+
if type == Array
|
406
|
+
args, arg = process_desttype_arg(args, opt_match, optional)
|
407
|
+
option_result[opt_def[:opt_dest]].push arg
|
408
|
+
elsif type == Hash
|
360
409
|
args, arg, key = process_desttype_hash_arg(args, opt_match, optional)
|
361
410
|
option_result[opt_def[:opt_dest]][key] = arg
|
362
411
|
end
|
363
|
-
else
|
364
|
-
args, arg = process_desttype_arg(args, opt_match, optional)
|
365
|
-
option_result[opt_def[:opt_dest]] = arg
|
366
412
|
end
|
367
|
-
[option_result, args]
|
368
413
|
end
|
369
414
|
|
370
415
|
def self.process_desttype_arg(args, opt_match, optional, required = false)
|
@@ -390,7 +435,7 @@ private
|
|
390
435
|
abort "[ERROR] missing argument for option '#{opt_match[0]}'!"
|
391
436
|
end
|
392
437
|
input = args.shift
|
393
|
-
if (matches =
|
438
|
+
if (matches = KEY_VALUE_REGEX.match(input))
|
394
439
|
key = matches[1]
|
395
440
|
arg = matches[2]
|
396
441
|
else
|
@@ -407,16 +452,52 @@ private
|
|
407
452
|
end
|
408
453
|
|
409
454
|
def self.integer?(obj)
|
410
|
-
obj.to_s
|
455
|
+
(INTEGER_REGEX =~ obj.to_s) == nil ? false : true
|
411
456
|
end
|
412
457
|
|
413
458
|
def self.numeric?(obj)
|
414
|
-
obj.to_s
|
459
|
+
(NUMERIC_REGEX =~ obj.to_s) == nil ? false : true
|
415
460
|
end
|
416
461
|
|
417
462
|
def self.option?(arg)
|
418
|
-
result = !!(arg
|
463
|
+
result = !!(IS_OPTION_REGEX =~ arg)
|
419
464
|
debug "Is option? '#{arg}' #{result}"
|
420
465
|
result
|
421
466
|
end
|
467
|
+
|
468
|
+
# Check if the given string is an option (begins with -).
|
469
|
+
# If the string is an option, it returns the options in that string as well
|
470
|
+
# as any arguments in it.
|
471
|
+
# @: s string, mode string
|
472
|
+
# return: options []string, argument string
|
473
|
+
def self.isOption?(s, mode)
|
474
|
+
# Handle special cases
|
475
|
+
if s == '--'
|
476
|
+
return ['--'], ''
|
477
|
+
elsif s == '-'
|
478
|
+
return ['-'], ''
|
479
|
+
end
|
480
|
+
options = Array.new
|
481
|
+
argument = String.new
|
482
|
+
matches = OPTION_REGEX.match(s)
|
483
|
+
if !matches.nil?
|
484
|
+
if matches[1] == '--'
|
485
|
+
options.push matches[2]
|
486
|
+
argument = matches[4]
|
487
|
+
else
|
488
|
+
case mode
|
489
|
+
when 'bundling'
|
490
|
+
options = matches[2].split('')
|
491
|
+
argument = matches[4]
|
492
|
+
when 'singleDash', 'single_dash', 'enforce_single_dash'
|
493
|
+
options.push matches[2][0].chr
|
494
|
+
argument = matches[2][1..-1] + matches[3] + matches[4]
|
495
|
+
else
|
496
|
+
options.push matches[2]
|
497
|
+
argument = matches[4]
|
498
|
+
end
|
499
|
+
end
|
500
|
+
end
|
501
|
+
return options, argument
|
502
|
+
end
|
422
503
|
end
|
metadata
CHANGED
@@ -1,44 +1,52 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-getoptions
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- David Gamba
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
11
|
+
|
12
|
+
date: 2015-06-27 00:00:00 Z
|
12
13
|
dependencies: []
|
14
|
+
|
13
15
|
description: The best looking option parser out there
|
14
16
|
email: davidgamba@gmail.com
|
15
17
|
executables: []
|
18
|
+
|
16
19
|
extensions: []
|
20
|
+
|
17
21
|
extra_rdoc_files: []
|
18
|
-
|
22
|
+
|
23
|
+
files:
|
19
24
|
- lib/ruby-getoptions.rb
|
20
25
|
homepage: https://github.com/DavidGamba/ruby-getoptions
|
21
|
-
licenses:
|
26
|
+
licenses:
|
22
27
|
- MIT
|
23
28
|
metadata: {}
|
29
|
+
|
24
30
|
post_install_message:
|
25
31
|
rdoc_options: []
|
26
|
-
|
32
|
+
|
33
|
+
require_paths:
|
27
34
|
- lib
|
28
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
-
requirements:
|
30
|
-
-
|
31
|
-
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
version: '0'
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- &id001
|
38
|
+
- ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- *id001
|
38
44
|
requirements: []
|
45
|
+
|
39
46
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
47
|
+
rubygems_version: 2.0.15
|
41
48
|
signing_key:
|
42
49
|
specification_version: 4
|
43
|
-
summary: Ruby option parser based on Perl
|
50
|
+
summary: "Ruby option parser based on Perl\xE2\x80\x99s Getopt::Long"
|
44
51
|
test_files: []
|
52
|
+
|