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.



558
559
560
# File 'lib/benry/cmdopt.rb', line 558

def initialize(schema)
  @schema = schema
end

Instance Method Details

#_error(msg) ⇒ Object



598
599
600
# File 'lib/benry/cmdopt.rb', line 598

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

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



562
563
564
565
566
567
568
569
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
# File 'lib/benry/cmdopt.rb', line 562

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