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.



566
567
568
# File 'lib/benry/cmdopt.rb', line 566

def initialize(schema)
  @schema = schema
end

Instance Method Details

#_error(msg) ⇒ Object



606
607
608
# File 'lib/benry/cmdopt.rb', line 606

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

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



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/benry/cmdopt.rb', line 570

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