class CommandKit::Options::OptionValue

Represents an additional argument associated with an option flag.

@api private

Constants

USAGES

Maps OptionParser types to USAGE strings.

Attributes

default[R]

The default parsed value for the argument value.

@return [Object, Proc, nil]

type[R]

The desired type of the argument value.

@return [Class, Hash, Array, Regexp, nil]

Public Class Methods

default_usage(type) click to toggle source

Returns the default option value usage for the given type.

@param [Class, Hash, Array, Regexp] type

The option value type.

@return [String, nil]

A default usage string based on the option value type.

@raise [TypeError]

The given type was not a Class, Hash, Array, or Regexp.
# File lib/command_kit/options/option_value.rb, line 92
def self.default_usage(type)
  USAGES.fetch(type) do
    case type
    when Class  then Inflector.underscore(type.name).upcase
    when Hash   then type.keys.join('|')
    when Array  then type.join('|')
    when Regexp then type.source
    else
      raise(TypeError,"unsupported option type: #{type.inspect}")
    end
  end
end
new(type: String, default: nil, usage: self.class.default_usage(type), **kwargs) click to toggle source

Initializes the option value.

@param [Class, Hash, Array, Regexp] type

The type of the option value.

@param [Object, Proc, nil] default

The default parsed value for the option value.

@param [String, nil] usage

The optional usage string for the option value.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [Boolean] required

Specifies whether the option value is required or optional.
# File lib/command_kit/options/option_value.rb, line 70
def initialize(type:    String,
               default: nil,
               usage:   self.class.default_usage(type),
               **kwargs)
  super(usage: usage, **kwargs)

  @type    = type
  @default = default
end

Public Instance Methods

default_value() click to toggle source

Returns a new default value.

@return [Object]

# File lib/command_kit/options/option_value.rb, line 121
def default_value
  if @default.respond_to?(:call) then @default.call
  else                                @default.dup
  end
end
usage() click to toggle source

The usage string for the argument.

@return [String, nil]

# File lib/command_kit/options/option_value.rb, line 110
def usage
  string = @usage
  string = "[#{string}]" if optional?
  string
end