class Slop::Option

Constants

DEFAULT_CONFIG

Attributes

block[R]

A custom proc that yields the option value when it's executed.

config[R]

A Hash of configuration options.

count[R]

An Integer count for the total times this option has been executed.

desc[R]

A custom description used for the help text.

flags[R]

An Array of flags this option matches.

value[W]

The end value for this option.

Public Class Methods

new(flags, desc, **config, &block) click to toggle source
# File lib/slop/option.rb, line 28
def initialize(flags, desc, **config, &block)
  @flags  = flags
  @desc   = desc
  @config = DEFAULT_CONFIG.merge(config)
  @block  = block
  reset
end

Public Instance Methods

call(_value) click to toggle source

This method is called immediately when an option is found. Override it in sub-classes.

# File lib/slop/option.rb, line 65
def call(_value)
  raise NotImplementedError,
    "you must override the `call' method for option #{self.class}"
end
default_value() click to toggle source

Returns the default value for this option (default is nil).

# File lib/slop/option.rb, line 94
def default_value
  config[:default]
end
ensure_call(value) click to toggle source

Since `call()` can be used/overriden in subclasses, this method is used to do general tasks like increment count. This ensures you don't have to call `super` when overriding `call()`. It's used in the Parser.

# File lib/slop/option.rb, line 47
def ensure_call(value)
  @count += 1

  if value.nil? && expects_argument?
    if default_value
      @value = default_value
    elsif !suppress_errors?
      raise Slop::MissingArgument.new("missing argument for #{flag}", flags)
    end
  else
    @value = call(value)
  end

  block.call(@value) if block.respond_to?(:call)
end
expects_argument?() click to toggle source

Override this if this option type does not expect an argument (i.e a boolean option type).

# File lib/slop/option.rb, line 78
def expects_argument?
  true
end
finish(_result) click to toggle source

By default this method does nothing. It's called when all options have been parsed and allows you to mutate the `@value` attribute according to other options.

# File lib/slop/option.rb, line 73
def finish(_result)
end
flag() click to toggle source

Returns all flags joined by a comma. Used by the help string.

# File lib/slop/option.rb, line 104
def flag
  flags.join(", ")
end
help?() click to toggle source

Returns true if this option should be displayed in help text.

# File lib/slop/option.rb, line 114
def help?
  config[:help]
end
key() click to toggle source

Returns the last key as a symbol. Used in Options.to_hash.

# File lib/slop/option.rb, line 109
def key
  (config[:key] || flags.last.sub(/\A--?/, '')).tr("-", "_").to_sym
end
null?() click to toggle source

Override this if you want to ignore the return value for an option (i.e so Slop::Result#to_hash does not include it).

# File lib/slop/option.rb, line 84
def null?
  false
end
reset() click to toggle source

Reset the option count and value. Used when calling .reset on the Parser.

# File lib/slop/option.rb, line 38
def reset
  @value = nil
  @count = 0
end
suppress_errors?() click to toggle source

Returns true if we should ignore errors that cause exceptions to be raised.

# File lib/slop/option.rb, line 99
def suppress_errors?
  config[:suppress_errors]
end
tail() click to toggle source

Returns 1 if this option should be added to the tail of the help text. Used for sorting.

# File lib/slop/option.rb, line 125
def tail
  tail? ? 1 : -1
end
tail?() click to toggle source

Returns true if this option should be added to the tail of the help text.

# File lib/slop/option.rb, line 119
def tail?
  config[:tail]
end
to_s(offset: 0) click to toggle source

Returns the help text for this option (flags and description).

# File lib/slop/option.rb, line 130
def to_s(offset: 0)
  "%-#{offset}s  %s" % [flag, desc]
end
value() click to toggle source

Returns the value for this option. Falls back to the default (or nil).

# File lib/slop/option.rb, line 89
def value
  @value || default_value
end