Exception: Toys::ArgParser::UsageError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/toys/arg_parser.rb

Overview

Base representation of a usage error reported by the ArgParser.

This is normally not raised directly, but returned as an element in the #errors array. It will, however, have the normal message and backtrace attributes, along with additional fields as defined in this class, and it can be raised later if desired.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, name: nil, value: nil, suggestions: nil, skip_frames: 0) ⇒ UsageError

Create a UsageError given a message and common data

Parameters:

  • message (String)

    The basic error message.

  • name (String, nil) (defaults to: nil)

    The name of the element (normally flag or positional argument) that reported the error, or nil if there is no definite element.

  • value (String, nil) (defaults to: nil)

    The value that was rejected, or nil if not applicable.

  • suggestions (Array<String>, nil) (defaults to: nil)

    An array of suggestions from DidYouMean, or nil if not applicable.

  • skip_frames (Integer) (defaults to: 0)

    Number of call frames to skip when constructing a backtrace, in addition to this initialize call itself. Subclasses calling super from their constructor should set this to 1 to skip their own initialize frame.



36
37
38
39
40
41
42
43
# File 'lib/toys/arg_parser.rb', line 36

def initialize(message, name: nil, value: nil, suggestions: nil, skip_frames: 0)
  super(message)
  @message = message
  @name = name
  @value = value
  @suggestions = suggestions
  ::Toys::Compat.set_backtrace(self, caller_locations(skip_frames + 1))
end

Instance Attribute Details

#messageString (readonly)

Returns The error message, not including any suggestions.

Returns:

  • (String)

    The error message, not including any suggestions.



48
49
50
# File 'lib/toys/arg_parser.rb', line 48

def message
  @message
end

#nameString? (readonly)

The name of the element (normally a flag or positional argument) that reported the error.

Returns:

  • (String)

    The element name.

  • (nil)

    if there is no definite element source.



57
58
59
# File 'lib/toys/arg_parser.rb', line 57

def name
  @name
end

#suggestionsArray<String>? (readonly)

An array of suggestions from DidYouMean.

Returns:

  • (Array<String>)

    array of suggestions.

  • (nil)

    if suggestions are not applicable to this error.



73
74
75
# File 'lib/toys/arg_parser.rb', line 73

def suggestions
  @suggestions
end

#valueString? (readonly)

The value that was rejected.

Returns:

  • (String)

    the value string

  • (nil)

    if a value is not applicable to this error.



65
66
67
# File 'lib/toys/arg_parser.rb', line 65

def value
  @value
end

Instance Method Details

#message_with_suggestionsString Also known as: to_s

A fully formatted error message including suggestions.

Returns:

  • (String)


80
81
82
83
84
85
86
87
# File 'lib/toys/arg_parser.rb', line 80

def message_with_suggestions
  if suggestions && !suggestions.empty?
    alts_str = suggestions.join("\n                 ")
    "#{@message}\nDid you mean...  #{alts_str}"
  else
    @message
  end
end