Method: Main::GetoptLong#get

Defined in:
lib/main/getoptlong.rb

#getObject Also known as: get_option

Get next option name and its argument as an array.



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/main/getoptlong.rb', line 290

def get
  option_name, option_argument = nil, ''

  #
  # Check status.
  #
  return nil if @error != nil
  case @status
  when STATUS_YET
    @status = STATUS_STARTED
  when STATUS_TERMINATED
    return nil
  end

  #
  # Get next option argument.
  #
  if 0 < @rest_singles.length
    argument = '-' + @rest_singles
  elsif (@argv.length == 0)
    terminate
    return nil
  elsif @ordering == PERMUTE
    while 0 < @argv.length && @argv[0] !~ /^-./
  @non_option_arguments.push(@argv.shift)
    end
    if @argv.length == 0
  terminate
  return nil
    end
    argument = @argv.shift
  elsif @ordering == REQUIRE_ORDER 
    if (@argv[0] !~ /^-./)
  terminate
  return nil
    end
    argument = @argv.shift
  else
    argument = @argv.shift
  end

  #
  # Check the special argument `--'.
  # `--' indicates the end of the option list.
  #
  if argument == '--' && @rest_singles.length == 0
    terminate
    return nil
  end

  #
  # Check for long and short options.
  #
  if argument =~ /^(--[^=]+)/ && @rest_singles.length == 0
    #
    # This is a long style option, which start with `--'.
    #
    pattern = $1
    if @canonical_names.include?(pattern)
  option_name = pattern
    else
  #
  # The option `option_name' is not registered in `@canonical_names'.
  # It may be an abbreviated.
  #
  match_count = 0
  @canonical_names.each_key do |key|
 if key.index(pattern) == 0
   option_name = key
   match_count += 1
 end
  end
  if 2 <= match_count
 set_error(AmbigousOption, "option `#{argument}' is ambiguous")
  elsif match_count == 0
 set_error(InvalidOption, "unrecognized option `#{argument}'")
  end
    end

    #
    # Check an argument to the option.
    #
    if @argument_flags[option_name] == REQUIRED_ARGUMENT
  if argument =~ /=(.*)$/
 option_argument = $1
  elsif 0 < @argv.length
 option_argument = @argv.shift
  else
 set_error(MissingArgument,
           "option `#{argument}' requires an argument")
  end
    elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
  if argument =~ /=(.*)$/
 option_argument = $1
  elsif 0 < @argv.length && @argv[0] !~ /^-./
 option_argument = @argv.shift
  else
 option_argument = ''
  end
    elsif argument =~ /=(.*)$/
  set_error(NeedlessArgument,
  "option `#{option_name}' doesn't allow an argument")
    end

  elsif argument =~ /^(-(.))(.*)/
    #
    # This is a short style option, which start with `-' (not `--').
    # Short options may be catinated (e.g. `-l -g' is equivalent to
    # `-lg').
    #
    option_name, ch, @rest_singles = $1, $2, $3

    if @canonical_names.include?(option_name)
  #
  # The option `option_name' is found in `@canonical_names'.
  # Check its argument.
  #
  if @argument_flags[option_name] == REQUIRED_ARGUMENT
 if 0 < @rest_singles.length
   option_argument = @rest_singles
   @rest_singles = ''
 elsif 0 < @argv.length
   option_argument = @argv.shift
 else
   # 1003.2 specifies the format of this message.
   set_error(MissingArgument, "option requires an argument -- #{ch}")
 end
  elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
 if 0 < @rest_singles.length
   option_argument = @rest_singles
   @rest_singles = ''
 elsif 0 < @argv.length && @argv[0] !~ /^-./
   option_argument = @argv.shift
 else
   option_argument = ''
 end
  end
    else
  #
  # This is an invalid option.
  # 1003.2 specifies the format of this message.
  #
  if ENV.include?('POSIXLY_CORRECT')
 set_error(InvalidOption, "illegal option -- #{ch}")
  else
 set_error(InvalidOption, "invalid option -- #{ch}")
  end
    end
  else
    #
    # This is a non-option argument.
    # Only RETURN_IN_ORDER falled into here.
    #
    return '', argument
  end

  return @canonical_names[option_name], option_argument
end