ruby-getoptions 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby-getoptions.rb +48 -35
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 191c3698f4c3883666210421afcdfbe7a6e2c904
4
- data.tar.gz: 4d5d296ebdc4f9dfb9012580b3c8220b5196a807
3
+ metadata.gz: 5ede0bd358c87652f4ee5eb78cc282494fb1cd98
4
+ data.tar.gz: 8458a240b73183ab4d91e7c3d84e9753a95e67cd
5
5
  SHA512:
6
- metadata.gz: 0a7bef2c3518bd10edf510b753156996871fe039bd89886a99843095e49994640718a87dec13b528b569556f632b44a18336d55184f1dc87dd957a766a214734
7
- data.tar.gz: 9efb629675196d862332a45d0fe589f56064eb8e554892d54b92e4771ce2ad2c013f384ddaf2968a391acc4864cbd967f7ef23209d765ee0c9c9d0a1c6443dd9
6
+ metadata.gz: 094485bd27102507986177274b9fb867d25e7bc171a354a5484d7a80bd293404a608db62cf28f28882996b6c2f4983daf6a1285394f923cd0bb316d456098788
7
+ data.tar.gz: 6b86f0b8b445ac1fc0ed45fc9d78c9fd772914c083ad41a5d36886e23d99925f640deb2aa5c2591bc33c94577d559146f5072e018eb223cd66793b1d7ef52ecd
@@ -20,6 +20,8 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
+ require 'logger'
24
+
23
25
  class GetOptions
24
26
  # argument_specification:
25
27
  # [ '',
@@ -38,14 +40,13 @@ class GetOptions
38
40
  def self.parse(args, option_map = {}, options = {})
39
41
  @options = options
40
42
  @option_map = {}
41
- @level = 2
42
43
  set_logging()
43
- info "input args: '#{args}'"
44
- info "input option_map: '#{option_map}'"
45
- info "input options: '#{options}'"
44
+ @log.info "input args: '#{args}'"
45
+ @log.info "input option_map: '#{option_map}'"
46
+ @log.info "input options: '#{options}'"
46
47
  @option_map = generate_extended_option_map(option_map)
47
48
  option_result, remaining_args = iterate_over_arguments(args, options[:mode])
48
- debug "option_result: '#{option_result}', remaining_args: '#{remaining_args}'"
49
+ @log.debug "option_result: '#{option_result}', remaining_args: '#{remaining_args}'"
49
50
  @log = nil
50
51
  [option_result, remaining_args]
51
52
  end
@@ -64,6 +65,7 @@ private
64
65
  REPEAT_REGEX = /\{(\d+)?(?:,\s?(\d+)?)?\}/
65
66
  NO_DEFINITION_REGEX = /^[=:+!]/
66
67
 
68
+
67
69
  # This is how the instance variable @option_map looks like:
68
70
  # @option_map:
69
71
  # {
@@ -75,24 +77,20 @@ private
75
77
  # }
76
78
  # }
77
79
 
78
- def self.info(msg)
79
- STDERR.puts "INFO |" + msg if @level <= 1
80
- end
81
-
82
- def self.debug(msg)
83
- STDERR.puts "DEBUG |" + msg if @level <= 0
84
- end
85
-
86
80
  def self.set_logging()
81
+ @log = Logger.new(STDERR)
82
+ @log.formatter = proc { |severity, datetime, progname, msg|
83
+ "#{severity} #{caller[3].split(':')[1]} #{msg}\n"
84
+ }
87
85
  case @options[:debug]
88
86
  when true
89
- @level = 0
87
+ @log.level = Logger::DEBUG
90
88
  when 'debug'
91
- @level = 0
89
+ @log.level = Logger::DEBUG
92
90
  when 'info'
93
- @level = 1
91
+ @log.level = Logger::INFO
94
92
  else
95
- @level = 2
93
+ @log.level = Logger::WARN
96
94
  end
97
95
  end
98
96
 
@@ -120,7 +118,7 @@ private
120
118
  definition_list.push(*definitions)
121
119
  end
122
120
  fail_on_duplicate_definitions(definition_list)
123
- debug "opt_map: #{opt_map}"
121
+ @log.debug "opt_map: #{opt_map}"
124
122
  opt_map
125
123
  end
126
124
 
@@ -207,12 +205,18 @@ private
207
205
  while args.size > 0
208
206
  arg = args.shift
209
207
  options, argument = isOption?(arg, mode)
208
+ @log.debug "arg: #{arg}, options: #{options}, argument: #{argument}"
210
209
  if options.size >= 1 && options[0] == '--'
211
210
  remaining_args.push(*args)
212
211
  return option_result, remaining_args
213
212
  elsif options.size >= 1
214
213
  option_result, remaining_args, args = process_option(arg, option_result, args, remaining_args, options, argument)
215
214
  else
215
+ # If require_order then push all to remaining once we see an arg that is not an option
216
+ if @options[:require_order]
217
+ remaining_args.push(arg, *args)
218
+ return option_result, remaining_args
219
+ end
216
220
  remaining_args.push arg
217
221
  end
218
222
  end
@@ -225,13 +229,17 @@ private
225
229
  opt_match, @option_map = find_option_matches(options[i])
226
230
  if opt_match.nil?
227
231
  remaining_args.push orig_opt
232
+ if @options[:require_order]
233
+ remaining_args.push(*args)
234
+ return option_result, remaining_args, []
235
+ end
228
236
  return option_result, remaining_args, args
229
237
  end
230
238
  # Only pass argument to the last option in the options array
231
239
  args.unshift argument unless argument.nil? || argument == "" || i < (options.size - 1)
232
- debug "new args: #{args}"
240
+ @log.debug "new args: #{args}"
233
241
  option_result, args = execute_option(opt_match, option_result, args)
234
- debug "option_result: #{option_result}"
242
+ @log.debug "option_result: #{option_result}"
235
243
  end
236
244
  return option_result, remaining_args, args
237
245
  end
@@ -254,7 +262,7 @@ private
254
262
  # Update the given hash
255
263
  hash[k][:negated] = true
256
264
  local_matches.push name
257
- debug "hash: #{hash}"
265
+ @log.debug "hash: #{hash}"
258
266
  end
259
267
  end
260
268
  end
@@ -278,14 +286,14 @@ private
278
286
  if @options[:fail_on_unknown]
279
287
  abort "[ERROR] Option '#{opt}' not found!"
280
288
  else
281
- debug "Option '#{opt}' not found!"
289
+ @log.debug "Option '#{opt}' not found!"
282
290
  $stderr.puts "[WARNING] Option '#{opt}' not found!" unless @options[:pass_through]
283
291
  return [nil, @option_map]
284
292
  end
285
293
  elsif matches.size > 1
286
294
  abort "[ERROR] option '#{opt}' matches multiple names '#{matches.sort.inspect}'!"
287
295
  end
288
- debug "matches: #{matches}"
296
+ @log.debug "matches: #{matches}"
289
297
  [matches[0], @option_map]
290
298
  end
291
299
 
@@ -293,13 +301,13 @@ private
293
301
  # Fail during init and not during run time.
294
302
  def self.execute_option(opt_match, option_result, args)
295
303
  opt_def = @option_map[opt_match]
296
- debug "#{opt_def[:arg_spec]}"
304
+ @log.debug "#{opt_def[:arg_spec]}"
297
305
  case opt_def[:arg_spec]
298
306
  when 'flag'
299
307
  if opt_def[:opt_dest].kind_of? Symbol
300
308
  option_result[opt_def[:opt_dest]] = true
301
309
  else
302
- debug "Flag definition is a function"
310
+ @log.debug "Flag definition is a function"
303
311
  opt_def[:opt_dest].call
304
312
  end
305
313
  when 'nflag'
@@ -374,7 +382,7 @@ private
374
382
  min = opt_def[:arg_opts][2][0]
375
383
  max = opt_def[:arg_opts][2][1]
376
384
  while min > 0
377
- debug "min: #{min}, max: #{max}"
385
+ @log.debug "min: #{min}, max: #{max}"
378
386
  min -= 1
379
387
  max -= 1
380
388
  abort "[ERROR] missing argument for option '#{opt_match[0]}'!" if args.size <= 0
@@ -387,7 +395,7 @@ private
387
395
  end
388
396
  end
389
397
  while max > 0
390
- debug "min: #{min}, max: #{max}"
398
+ @log.debug "min: #{min}, max: #{max}"
391
399
  max -= 1
392
400
  break if args.size <= 0
393
401
  if type == Array
@@ -412,8 +420,13 @@ private
412
420
  end
413
421
 
414
422
  def self.process_desttype_arg(args, opt_match, optional, required = false)
415
- if !args[0].nil? && option?(args[0])
416
- debug "args[0] option"
423
+ # If this arg exists, is required, and is string type, just use it
424
+ if !args[0].nil? &&
425
+ @option_map[opt_match][:arg_opts][0] == 's' &&
426
+ !optional
427
+ arg = process_option_type(args.shift, opt_match, optional)
428
+ elsif !args[0].nil? && option?(args[0])
429
+ @log.debug "args[0] option"
417
430
  if required
418
431
  return args, nil
419
432
  end
@@ -421,9 +434,9 @@ private
421
434
  else
422
435
  arg = process_option_type(args.shift, opt_match, optional)
423
436
  end
424
- debug "arg: '#{arg}'"
437
+ @log.debug "arg: '#{arg}'"
425
438
  if arg.nil?
426
- debug "arg is nil"
439
+ @log.debug "arg is nil"
427
440
  abort "[ERROR] missing argument for option '#{opt_match[0]}'!"
428
441
  end
429
442
  [args, arg]
@@ -440,11 +453,11 @@ private
440
453
  else
441
454
  abort "[ERROR] argument for option '#{opt_match[0]}' must be of type key=value!"
442
455
  end
443
- debug "key: '#{key}', arg: '#{arg}'"
456
+ @log.debug "key: '#{key}', arg: '#{arg}'"
444
457
  arg = process_option_type(arg, opt_match, optional)
445
- debug "arg: '#{arg}'"
458
+ @log.debug "arg: '#{arg}'"
446
459
  if arg.nil?
447
- debug "arg is nil"
460
+ @log.debug "arg is nil"
448
461
  abort "[ERROR] missing argument for option '#{opt_match[0]}'!"
449
462
  end
450
463
  [args, arg, key]
@@ -460,7 +473,7 @@ private
460
473
 
461
474
  def self.option?(arg)
462
475
  result = !!(IS_OPTION_REGEX =~ arg)
463
- debug "Is option? '#{arg}' #{result}"
476
+ @log.debug "Is option? '#{arg}' #{result}"
464
477
  result
465
478
  end
466
479
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-getoptions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Gamba
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-21 00:00:00.000000000 Z
11
+ date: 2016-11-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby option parser based on Perl’s Getopt::Long
14
14
  email: davidgamba@gmail.com