Exception: OptionParser::ParseError

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/optparse.rb

Overview

Base class of exceptions from OptionParser.

Constant Summary collapse

Reason =

Reason which caused the error.

'parse error'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, additional: nil) ⇒ ParseError

Returns a new instance of ParseError.



2030
2031
2032
2033
2034
2035
# File 'lib/optparse.rb', line 2030

def initialize(*args, additional: nil)
  @additional = additional
  @arg0, = args
  @args = args
  @reason = nil
end

Instance Attribute Details

#additionalObject

Returns the value of attribute additional.



2039
2040
2041
# File 'lib/optparse.rb', line 2039

def additional
  @additional
end

#argsObject (readonly)

Returns the value of attribute args.



2037
2038
2039
# File 'lib/optparse.rb', line 2037

def args
  @args
end

#reasonObject

Returns error reason. Override this for I18N.



2072
2073
2074
# File 'lib/optparse.rb', line 2072

def reason
  @reason || self.class::Reason
end

Class Method Details

.filter_backtrace(array) ⇒ Object



2049
2050
2051
2052
2053
2054
# File 'lib/optparse.rb', line 2049

def self.filter_backtrace(array)
  unless $DEBUG
    array.delete_if(&%r"\A#{Regexp.quote(__FILE__)}:"o.method(:=~))
  end
  array
end

Instance Method Details

#inspectObject



2076
2077
2078
# File 'lib/optparse.rb', line 2076

def inspect
  "#<#{self.class}: #{args.join(' ')}>"
end

#messageObject Also known as: to_s

Default stringizing method to emit standard error message.



2083
2084
2085
# File 'lib/optparse.rb', line 2083

def message
  "#{reason}: #{args.join(' ')}#{additional[@arg0] if additional}"
end

#recover(argv) ⇒ Object

Pushes back erred argument(s) to argv.



2044
2045
2046
2047
# File 'lib/optparse.rb', line 2044

def recover(argv)
  argv[0, 0] = @args
  argv
end

#set_backtrace(array) ⇒ Object



2056
2057
2058
# File 'lib/optparse.rb', line 2056

def set_backtrace(array)
  super(self.class.filter_backtrace(array))
end

#set_option(opt, eq) ⇒ Object



2060
2061
2062
2063
2064
2065
2066
2067
# File 'lib/optparse.rb', line 2060

def set_option(opt, eq)
  if eq
    @args[0] = opt
  else
    @args.unshift(opt)
  end
  self
end