Class: Toys::ArgParser::UsageError

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

Overview

Base representation of a usage error reported by the ArgParser.

This functions similarly to an exception, but is not raised. Rather, it is returned in the #errors array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, name: nil, value: nil, suggestions: nil) ⇒ 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.



30
31
32
33
34
35
# File 'lib/toys/arg_parser.rb', line 30

def initialize(message, name: nil, value: nil, suggestions: nil)
  @message = message
  @name = name
  @value = value
  @suggestions = suggestions
end

Instance Attribute Details

#messageString (readonly)

The basic error message. Does not include suggestions, if any.

Returns:

  • (String)


42
43
44
# File 'lib/toys/arg_parser.rb', line 42

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.



51
52
53
# File 'lib/toys/arg_parser.rb', line 51

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.



67
68
69
# File 'lib/toys/arg_parser.rb', line 67

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.



59
60
61
# File 'lib/toys/arg_parser.rb', line 59

def value
  @value
end

Instance Method Details

#full_messageString Also known as: to_s

A fully formatted error message including suggestions.

Returns:

  • (String)


74
75
76
77
78
79
80
81
# File 'lib/toys/arg_parser.rb', line 74

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