class Slop::Option
Constants
- DEFAULT_CONFIG
Attributes
A custom proc that yields the option value when it's executed.
A Hash of configuration options.
An Integer count for the total times this option has been executed.
A custom description used for the help text.
An Array of flags this option matches.
The end value for this option.
Public Class Methods
# File lib/slop/option.rb, line 30 def initialize(flags, desc, **config, &block) @flags = flags @desc = desc @config = DEFAULT_CONFIG.merge(config) @block = block reset end
Public Instance Methods
This method is called immediately when an option is found. Override it in sub-classes.
# File lib/slop/option.rb, line 71 def call(_value) raise NotImplementedError, "you must override the `call' method for option #{self.class}" end
Returns the default value for this option (default is nil).
# File lib/slop/option.rb, line 100 def default_value config[:default] end
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 49 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 if validate_type? && !valid?(value) && !suppress_errors? raise Slop::InvalidOptionValue.new("invalid value for #{flag}", flags) end @value = valid?(value) && call(value) end block.call(@value) if block.respond_to?(:call) end
Override this if this option type does not expect an argument (i.e a boolean option type).
# File lib/slop/option.rb, line 84 def expects_argument? true end
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 79 def finish(_result) end
Returns all flags joined by a comma. Used by the help string.
# File lib/slop/option.rb, line 122 def flag flags.join(", ") end
Returns true if this option should be displayed in help text.
# File lib/slop/option.rb, line 146 def help? config[:help] end
Returns the last key as a symbol. Used in Options.to_hash.
# File lib/slop/option.rb, line 127 def key key = config[:key] || flags.last.sub(/\A--?/, '') key = key.tr '-', '_' if underscore_flags? key.to_sym end
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 90 def null? false end
Returns true if an exception should be raised when this option isn't supplied.
# File lib/slop/option.rb, line 110 def required? config[:required] end
Reset the option count and value. Used when calling .reset on the Parser.
# File lib/slop/option.rb, line 40 def reset @value = nil @count = 0 end
Returns true if we should ignore errors that cause exceptions to be raised.
# File lib/slop/option.rb, line 105 def suppress_errors? config[:suppress_errors] end
Returns 1 if this option should be added to the tail of the help text. Used for sorting.
# File lib/slop/option.rb, line 157 def tail tail? ? 1 : -1 end
Returns true if this option should be added to the tail of the help text.
# File lib/slop/option.rb, line 151 def tail? config[:tail] end
Returns the help text for this option (flags and description).
# File lib/slop/option.rb, line 162 def to_s(offset: 0) "%-#{offset}s %s" % [flag, desc] end
Returns true if this option should be displayed with dashes transformed into underscores.
# File lib/slop/option.rb, line 141 def underscore_flags? config[:underscore_flags] end
Override this if you want to provide a custom validator for a type. This method must return whether the provided value is valid for the current argument's type
# File lib/slop/option.rb, line 136 def valid?(value) true end
Returns true if an exception should be raised when this option value can't be parsed into the desired type or does not conform to the expected type's format
# File lib/slop/option.rb, line 117 def validate_type? config[:validate_type] || config[:validate_types] end
Returns the value for this option. Falls back to the default (or nil).
# File lib/slop/option.rb, line 95 def value @value || default_value end