class Benry::Cmdopt::Parser

Public Class Methods

new(schema) click to toggle source
# File lib/benry/cmdopt.rb, line 401
def initialize(schema)
  @schema = schema
end

Public Instance Methods

error(msg) click to toggle source
# File lib/benry/cmdopt.rb, line 430
def error(msg)
  return OptionError.new(msg)
end
parse(argv) { |ex| ... } click to toggle source
# File lib/benry/cmdopt.rb, line 405
def parse(argv, &error_handler)
  optdict = new_options_dict()
  while !argv.empty? && argv[0] =~ /\A-/
    optstr = argv.shift
    #; [!y04um] skips rest options when '--' found in argv.
    if optstr == '--'
      break
    elsif optstr =~ /\A--/
      #; [!uh7j8] parses long options.
      parse_long_option(optstr, optdict, argv)
    else
      #; [!nwnjc] parses short options.
      parse_short_options(optstr, optdict, argv)
    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

Protected Instance Methods

new_options_dict() click to toggle source
# File lib/benry/cmdopt.rb, line 501
def new_options_dict()
  #; [!vm6h0] returns new hash object.
  return OPTIONS_CLASS.new
end
parse_long_option(optstr, optdict, _argv) click to toggle source
# File lib/benry/cmdopt.rb, line 436
def parse_long_option(optstr, optdict, _argv)
  #; [!3i994] raises OptionError when invalid long option format.
  optstr =~ /\A--(\w[-\w]*)(?:=(.*))?\z/  or
    raise error("#{optstr}: invalid long option.")
  name = $1; val = $2
  #; [!er7h4] raises OptionError when unknown long option.
  item = @schema.find_long_option(name)  or
    raise error("#{optstr}: unknown long option.")
  #; [!2jd9w] raises OptionError when no arguments specified for arg required long option.
  #; [!qyq8n] raises optionError when an argument specified for no arg long option.
  if item.optional_param?
    # do nothing
  elsif item.param
    val  or raise error("#{optstr}: argument required.")
  else
    val.nil?  or raise error("#{optstr}: unexpected argument.")
  end
  #; [!o596x] validates argument value.
  val ||= true
  begin
    val = item.validate_and_convert(val, optdict)
  rescue RuntimeError => ex
    raise error("#{optstr}: #{ex.message}")
  end
  optdict[item.key] = val
end
parse_short_options(optstr, optdict, argv) click to toggle source
# File lib/benry/cmdopt.rb, line 463
def parse_short_options(optstr, optdict, argv)
  n = optstr.length
  i = 0
  while (i += 1) < n
    char = optstr[i]
    #; [!4eh49] raises OptionError when unknown short option specified.
    item = @schema.find_short_option(char)  or
      raise error("-#{char}: unknown option.")
    #
    if !item.param
      val = true
    elsif !item.optional_param?
      #; [!utdbf] raises OptionError when argument required but not specified.
      #; [!f63hf] short option arg can be specified without space separator.
      val = i+1 < n ? optstr[(i+1)..-1] : argv.shift  or
        raise error("-#{char}: argument required.")
      i = n
    else
      #; [!yjq6b] optional arg should be specified without space separator.
      #; [!wape4] otpional arg can be omit.
      val = i+1 < n ? optstr[(i+1)..-1] : true
      i = n
    end
    #; [!yu0kc] validates short option argument.
    begin
      val = item.validate_and_convert(val, optdict)
    rescue RuntimeError => ex
      if val == true
        raise error("-#{char}: #{ex.message}")
      else
        s = item.optional_param? ? '' : ' '
        raise error("-#{char}#{s}#{val}: #{ex.message}")
      end
    end
    optdict[item.key] = val
  end
end