Class: Benry::CmdOpt::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/benry/cmdopt.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Parser

Returns a new instance of Parser.



506
507
508
# File 'lib/benry/cmdopt.rb', line 506

def initialize(schema)
  @schema = schema
end

Instance Method Details

#_error(msg) ⇒ Object



546
547
548
# File 'lib/benry/cmdopt.rb', line 546

def _error(msg)
  return OptionError.new(msg)
end

#parse(argv, all: true, &error_handler) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/benry/cmdopt.rb', line 510

def parse(argv, all: true, &error_handler)
  optdict = new_options_dict()
  index = 0
  while index < argv.length
    #; [!5s5b6] treats '-' as an argument, not an option.
    if argv[index] =~ /\A-/ && argv[index] != "-"
      optstr = argv.delete_at(index)
    #; [!q8356] parses options even after arguments when `all: true`.
    elsif all
      index += 1
      next
    #; [!ryra3] doesn't parse options after arguments when `all: false`.
    else
      break
    end
    #; [!y04um] skips rest options when '--' found in argv.
    if optstr == '--'
      break
    elsif optstr =~ /\A--/
      #; [!uh7j8] parses long options.
      parse_long_option(optstr, optdict)
    else
      #; [!nwnjc] parses short options.
      parse_short_options(optstr, optdict) { argv.delete_at(index) }
    end
  end
  #; [!3wmsy] returns command option values as a dict.
  return optdict
rescue OptionError => ex
  #; [!qpuxh] handles only OptionError when block given.
  raise unless block_given?()
  yield ex
  #; [!dhpw1] returns nil when OptionError handled.
  nil
end