Class: TTY::Option::InvalidArity

Inherits:
ParseError show all
Defined in:
lib/tty/option/errors.rb

Overview

Raised when number of parameter arguments doesn’t match

Constant Summary collapse

MESSAGE =
"%<type>s '%<name>s' should appear %<expect>s but appeared %<actual>s"

Instance Attribute Summary

Attributes inherited from ParseError

#param

Instance Method Summary collapse

Methods inherited from ParseError

#format_value

Constructor Details

#initialize(param_or_message, arity = nil) ⇒ InvalidArity

Returns a new instance of InvalidArity.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tty/option/errors.rb', line 73

def initialize(param_or_message, arity = nil)
  if param_or_message.is_a?(Parameter)
    @param = param_or_message
    prefix = param.arity < 0 ? "at least " : ""
    expected_arity = param.arity < 0 ? param.arity.abs - 1 : param.arity

    message = format(MESSAGE,
                     type: param.to_sym,
                     name: param.name,
                     expect: prefix + pluralize("time", expected_arity),
                     actual: pluralize("time", arity))
  else
    message = param_or_message
  end

  super(message)
end

Instance Method Details

#pluralize(noun, count = 1) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pluralize a noun



94
95
96
# File 'lib/tty/option/errors.rb', line 94

def pluralize(noun, count = 1)
  "#{count} #{noun}#{'s' unless count == 1}"
end