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.



530
531
532
# File 'lib/benry/cmdopt.rb', line 530

def initialize(schema)
  @schema = schema
end

Instance Method Details

#_error(msg) ⇒ Object



570
571
572
# File 'lib/benry/cmdopt.rb', line 570

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

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



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/benry/cmdopt.rb', line 534

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