Module: RakeCommander::Options::Error::Handling::ClassMethods

Defined in:
lib/rake-commander/options/error/handling.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#options_latest_errorObject (readonly)

Returns the value of attribute options_latest_error.



16
17
18
# File 'lib/rake-commander/options/error/handling.rb', line 16

def options_latest_error
  @options_latest_error
end

Instance Method Details

#error_on_leftovers(action = :not_used, &handler) ⇒ Object

See Also:



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

def error_on_leftovers(action = :not_used, &handler)
  error_on_options(action, error: RakeCommander::Options::Error::UnknownArgument, &handler)
end

#error_on_options(action = :not_used, error: RakeCommander::Options::Error::Base) {|error, argv, results, leftovers| ... } ⇒ Boolean

Note:
  1. It triggers error by default when there are parsing option errors.
  2. Even if a handler block is defined, if action is false it won't trigger error.
  3. When specific errors are NOT specified, they will fallback to the action defined on the parent class RakeCommander::Options::Error::Base. This means that you can define a default behaviour for this.

Whether it should trigger an error when there are ARGV option errors during parse_options

Parameters:

  • action (Boolean, Symbol) (defaults to: :not_used)

    possible values are:

    1. :not_used -> it will retrieve the currect action value
    2. true (default) -> it switches on the exception triggering
    3. false -> it will print the error and exit with status 1
    4. :continue -> it will continue with whatver it got (use this at your own risk)
  • error (RakeCommander::Options::Error::Base:Class) (defaults to: RakeCommander::Options::Error::Base)

    or children thereof.

Yields:

  • (error, argv, results, leftovers)

    do some stuff and decide if an error should be raised.

Yield Parameters:

  • error (RakeCommander::Options::Error::Base)

    the specific error.

  • argv (Array<String>)

    arguments that were being parsed.

  • results (Hash)

    the parsed options.

  • leftovers (Array<String>)

    arguments of argv that the parser could not identify.

Yield Returns:

  • (Boolean)

    whether this should trigger an error or not.

Returns:

  • (Boolean)

    whether this error is enabled.

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rake-commander/options/error/handling.rb', line 41

def error_on_options(action = :not_used, error: RakeCommander::Options::Error::Base, &handler)
  RakeCommander::Options::Error::Base.require_argument!(error, :error, accept_children: true)
  @options_latest_error       = nil
  @error_on_options         ||= {}
  @error_on_options[error]    = action if action != :not_used

  if block_given?
    error_on_options_handler(error, &handler)
    @error_on_options[error] ||= true
  end

  return self unless block_given? || action != :not_used
  # default value
  @error_on_options[error] = true unless @error_on_options[error] == false
  @error_on_options[error]
end

#error_on_options?(error = RakeCommander::Options::Error::Base) ⇒ Boolean

Returns whether there is an error action defined for error.

Returns:

  • (Boolean)

    whether there is an error action defined for error



64
65
66
67
68
# File 'lib/rake-commander/options/error/handling.rb', line 64

def error_on_options?(error = RakeCommander::Options::Error::Base)
  RakeCommander::Options::Error::Base.require_argument!(error, :error, accept_children: true)
  _default_action = error_on_options
  @error_on_options.key?(error) || error_on_options_handler.key?(error)
end