class CommandKit::Options::Option
Represents a defined option.
@api private
Attributes
The optional block that will receive the parsed option value.
@return [Proc, nil]
The option's long-flag.
@return [String]
The option's name.
@return [Symbol]
The option's optional short-flag.
@return [String, nil]
The option value's type.
@return [OptionValue, nil]
Public Class Methods
The default long option (ex: `–long-opt`) for the given option name (ex: `:long_opt`).
@param [Symbol] name
The option name.
@return [String]
The long-flag for the option.
# File lib/command_kit/options/option.rb, line 112 def self.default_long_opt(name) "--#{Inflector.dasherize(name)}" end
Initializes the option.
@param [Symbol] name
The name of the option.
@param [String, nil] short
Optional short-flag for the option.
@param [String, nil] long
Optional explicit long-flag for the option.
@param [Boolean] equals
Specifies whether the option is of the form (`--opt=value`).
@param [Hash{Symbol => Object}, true, false, nil] value
Keyword arguments for {OptionValue#initialize}, or `nil` if the option has no additional value.
@option value [Class, Hash, Array, Regexp] type
The type of the option's value.
@option value [String, nil] usage
The usage string for the option's value.
@param [String] desc
The description for the option.
@yield [(value)]
If a block is given, it will be called when the option is parsed.
@yieldparam [Object, nil] value
The given block will be passed the parsed option's value.
@raise [TypeError]
The `value` keyword argument was not a `Hash`, `true`, `false`, or `nil`.
# File lib/command_kit/options/option.rb, line 81 def initialize(name, short: nil, long: self.class.default_long_opt(name), equals: false, value: nil, desc: , &block) @name = name @short = short @long = long @equals = equals @value = case value when Hash then OptionValue.new(**value) when true then OptionValue.new() when false, nil then nil else raise(TypeError,"value: keyword must be Hash, true, false, or nil") end @desc = desc @block = block end
Public Instance Methods
The option value's default value.
@return [Object, nil]
@see OptionValue#default_value
# File lib/command_kit/options/option.rb, line 175 def default_value @value && @value.default_value end
The option description.
@return [String]
@note
If {#default_value} returns a value, the description will contain the `Default:` value the option will be initialized with.
# File lib/command_kit/options/option.rb, line 188 def desc if (value = default_value) "#{@desc} (Default: #{value})" else @desc end end
Specifies if the option is of the form `–opt=VALUE`.
@return [Boolean]
# File lib/command_kit/options/option.rb, line 121 def equals? @equals end
The separator character between the option and option value.
@return ['=', ' ', nil]
# File lib/command_kit/options/option.rb, line 130 def separator if @value if equals? then '=' else ' ' end end end
The option value's type.
@return [Class, nil]
@see OptionValue#type
# File lib/command_kit/options/option.rb, line 164 def type @value && @value.type end
Usage
strings for the option.
@return [Array<String>]
The usage strings.
# File lib/command_kit/options/option.rb, line 144 def usage [*@short, "#{@long}#{separator}#{@value && @value.usage}"] end
Determines if the option has a value.
@return [Boolean]
# File lib/command_kit/options/option.rb, line 153 def value? !@value.nil? end