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'
DIR =
File.join(__dir__, '')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

:nodoc:



2252
2253
2254
2255
2256
2257
# File 'lib/optparse.rb', line 2252

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

Instance Attribute Details

#additionalObject

Returns the value of attribute additional.



2261
2262
2263
# File 'lib/optparse.rb', line 2261

def additional
  @additional
end

#argsObject (readonly)

Returns the value of attribute args.



2259
2260
2261
# File 'lib/optparse.rb', line 2259

def args
  @args
end

#reasonObject

Returns error reason. Override this for I18N.



2295
2296
2297
# File 'lib/optparse.rb', line 2295

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

Class Method Details

.filter_backtrace(array) ⇒ Object



2272
2273
2274
2275
2276
2277
# File 'lib/optparse.rb', line 2272

def self.filter_backtrace(array)
  unless $DEBUG
    array.delete_if {|bt| bt.start_with?(DIR)}
  end
  array
end

Instance Method Details

#inspectObject



2299
2300
2301
# File 'lib/optparse.rb', line 2299

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

#messageObject Also known as: to_s

Default stringizing method to emit standard error message.



2306
2307
2308
# File 'lib/optparse.rb', line 2306

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

#recover(argv) ⇒ Object

Pushes back erred argument(s) to argv.



2266
2267
2268
2269
# File 'lib/optparse.rb', line 2266

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

#set_backtrace(array) ⇒ Object



2279
2280
2281
# File 'lib/optparse.rb', line 2279

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

#set_option(opt, eq) ⇒ Object



2283
2284
2285
2286
2287
2288
2289
2290
# File 'lib/optparse.rb', line 2283

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