Exception: RakeCommander::Options::Error::Base

Inherits:
Base::CustomError show all
Extended by:
Name
Defined in:
lib/rake-commander/options/error/base.rb

Overview

Base error class that does a rely between OptionParser and RakeCommander errors

Constant Summary collapse

OPTION_REGEX =
/(?:argument|option): (?<option>.+)/i

Constants included from Name

Name::BOOLEAN_NAME_REGEX, Name::BOOLEAN_TOKEN, Name::DOUBLE_HYPHEN_REGEX, Name::HYPEN_REGEX, Name::HYPHEN_START_REGEX, Name::OPTIONAL_REGEX, Name::SINGLE_HYPHEN_REGEX, Name::UNDERSCORE_REGEX, Name::WORD_DELIMITER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Name

argument_optional?, argument_required?, boolean_name?, capture_argument_with!, capture_arguments_name!, capture_arguments_short!, double_hyphen?, name_argument, name_argument?, name_hyphen, name_sym, name_word_sym, short_hyphen, short_sym, single_hyphen?, valid_name?, valid_short?

Methods inherited from Base::CustomError

#message, #to_s

Constructor Details

#initialize(value = nil, from: nil, option: nil) ⇒ Base

Returns a new instance of Base.



40
41
42
43
44
# File 'lib/rake-commander/options/error/base.rb', line 40

def initialize(value = nil, from: nil, option: nil)
  @from   = from
  @option = option
  super(value)
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



38
39
40
# File 'lib/rake-commander/options/error/base.rb', line 38

def from
  @from
end

#optionObject (readonly)

Returns the value of attribute option.



38
39
40
# File 'lib/rake-commander/options/error/base.rb', line 38

def option
  @option
end

Class Method Details

.option_regex(value = :not_used) ⇒ Object

To (re)define the RegExp used to identify the option of an error message.



20
21
22
23
24
25
# File 'lib/rake-commander/options/error/base.rb', line 20

def option_regex(value = :not_used)
  @option_regex ||= OPTION_REGEX
  return @option_regex if value == :not_used

  @option_regex = value
end

.option_sym(message) ⇒ Object

Identifies the option Symbol (short or name) for a given message



28
29
30
31
32
33
34
35
# File 'lib/rake-commander/options/error/base.rb', line 28

def option_sym(message)
  return unless (match = message.match(option_regex))

  option = match[:option]
  return name_word_sym(option) if option.length > 1

  short_sym(option)
end

.require_argument!(error, arg_name, accept_children: true) ⇒ Object

Helper to check if error is this class or any children class

Raises:

  • ArgumentError if it does not meet this condition.



12
13
14
15
16
17
# File 'lib/rake-commander/options/error/base.rb', line 12

def require_argument!(error, arg_name, accept_children: true)
  msg  = "Expecting #{arg_name} to be #{self}"
  msg << "or child thereof." if accept_children
  msg << ". Given: #{error.is_a?(Class)? error : error.class}"
  raise ArgumentError, msg unless error <= self
end

Instance Method Details

#from_descObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rake-commander/options/error/base.rb', line 63

def from_desc
  return '' unless from

  if from.respond_to?(:name)
    "(#{from.name}) "
  elsif from.respond_to?(:to_s)
    "(#{from}) "
  else
    ''
  end
end

#name?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rake-commander/options/error/base.rb', line 51

def name?
  option_sym.to_s.length > 1
end

#option_symObject



59
60
61
# File 'lib/rake-commander/options/error/base.rb', line 59

def option_sym
  @option_sym ||= self.class.option_sym(message)
end

#optionsObject

Options that are related to the error. There may not be any.



47
48
49
# File 'lib/rake-commander/options/error/base.rb', line 47

def options
  [option].compact.flatten
end

#short?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rake-commander/options/error/base.rb', line 55

def short?
  option_sym.to_s.length == 1
end