Class: Toys::ArgParser::UsageError

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

Overview

Defined in the toys-core gem

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.



34
35
36
37
38
39
# File 'toys-core/lib/toys/arg_parser.rb', line 34

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)


46
47
48
# File 'toys-core/lib/toys/arg_parser.rb', line 46

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.



55
56
57
# File 'toys-core/lib/toys/arg_parser.rb', line 55

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.



71
72
73
# File 'toys-core/lib/toys/arg_parser.rb', line 71

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.



63
64
65
# File 'toys-core/lib/toys/arg_parser.rb', line 63

def value
  @value
end

Instance Method Details

#full_messageString Also known as: to_s

A fully formatted error message including suggestions.

Returns:

  • (String)


78
79
80
81
82
83
84
85
# File 'toys-core/lib/toys/arg_parser.rb', line 78

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