class Toys::ArgParser::UsageError

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 {Toys::ArgParser#errors} array.

Attributes

message[R]

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

@return [String]

name[R]

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

@return [String] The element name. @return [nil] if there is no definite element source.

suggestions[R]

An array of suggestions from DidYouMean.

@return [Array<String>] array of suggestions. @return [nil] if suggestions are not applicable to this error.

value[R]

The value that was rejected.

@return [String] the value string @return [nil] if a value is not applicable to this error.

Public Class Methods

new(message, name: nil, value: nil, suggestions: nil) click to toggle source

Create a UsageError given a message and common data

@param message [String] The basic error message. @param name [String,nil] The name of the element (normally flag or

positional argument) that reported the error, or nil if there is
no definite element.

@param value [String,nil] The value that was rejected, or nil if not

applicable.

@param suggestions [Array<String>,nil] An array of suggestions from

DidYouMean, or nil if not applicable.
# 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

Public Instance Methods

full_message() click to toggle source

A fully formatted error message including suggestions.

@return [String]

# 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
Also aliased as: to_s
to_s()
Alias for: full_message