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.freeze

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.



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

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.



35
36
37
# File 'lib/rake-commander/options/error/base.rb', line 35

def from
  @from
end

#optionObject (readonly)

Returns the value of attribute option.



35
36
37
# File 'lib/rake-commander/options/error/base.rb', line 35

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
# 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



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

def option_sym(message)
  return nil 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



60
61
62
63
64
65
66
67
68
69
# File 'lib/rake-commander/options/error/base.rb', line 60

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)


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

def name?
  option_sym.to_s.length > 1
end

#option_symObject



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

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

#optionsObject

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



44
45
46
# File 'lib/rake-commander/options/error/base.rb', line 44

def options
  [option].compact.flatten
end

#short?Boolean

Returns:

  • (Boolean)


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

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