Class: CommandKit::Options::Option Private

Inherits:
Object
  • Object
show all
Defined in:
lib/command_kit/options/option.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents a defined option.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, short: nil, long: self.class.default_long_opt(name), equals: false, value: nil, desc:, category: nil) {|(value)| ... } ⇒ Option

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the option.

Parameters:

  • name (Symbol)

    The name of the option.

  • short (String, nil) (defaults to: nil)

    Optional short-flag for the option.

  • long (String, nil) (defaults to: self.class.default_long_opt(name))

    Optional explicit long-flag for the option.

  • equals (Boolean) (defaults to: false)

    Specifies whether the option is of the form (--opt=value).

  • value (Hash{Symbol => Object}, true, false, nil) (defaults to: nil)

    Keyword arguments for CommandKit::Options::OptionValue#initialize, or nil if the option has no additional value.

  • desc (String, Array<String>)

    The description for the option.

  • category (String, nil) (defaults to: nil)

    The optional category to group the option under.

Options Hash (value:):

  • type (Class, Hash, Array, Regexp)

    The type of the option's value.

  • usage (String, nil)

    The usage string for the option's value.

Yields:

  • ((value))

    If a block is given, it will be called when the option is parsed.

Yield Parameters:

  • value (Object, nil)

    The given block will be passed the parsed option's value.

Raises:

  • (TypeError)

    The value keyword argument was not a Hash, true, false, or nil.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/command_kit/options/option.rb', line 93

def initialize(name, short:    nil,
                     long:     self.class.default_long_opt(name),
                     equals:   false,
                     value:    nil,
                     desc:     ,
                     category: nil,
                     &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
  @category = category
  @block    = block
end

Instance Attribute Details

#blockProc? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The optional block that will receive the parsed option value.

Returns:

  • (Proc, nil)


43
44
45
# File 'lib/command_kit/options/option.rb', line 43

def block
  @block
end

#categoryString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The optional category to group the option under.

Returns:

  • (String, nil)

Since:

  • 0.3.0



50
51
52
# File 'lib/command_kit/options/option.rb', line 50

def category
  @category
end

#longString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option's long-flag.

Returns:

  • (String)


33
34
35
# File 'lib/command_kit/options/option.rb', line 33

def long
  @long
end

#nameSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option's name.

Returns:

  • (Symbol)


23
24
25
# File 'lib/command_kit/options/option.rb', line 23

def name
  @name
end

#shortString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option's optional short-flag.

Returns:

  • (String, nil)


28
29
30
# File 'lib/command_kit/options/option.rb', line 28

def short
  @short
end

#valueOptionValue? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option value's type.

Returns:



38
39
40
# File 'lib/command_kit/options/option.rb', line 38

def value
  @value
end

Class Method Details

.default_long_opt(name) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The default long option (ex: --long-opt) for the given option name (ex: :long_opt).

Parameters:

  • name (Symbol)

    The option name.

Returns:

  • (String)

    The long-flag for the option.



126
127
128
# File 'lib/command_kit/options/option.rb', line 126

def self.default_long_opt(name)
  "--#{Inflector.dasherize(name)}"
end

Instance Method Details

#default_valueObject?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option value's default value.

Returns:

  • (Object, nil)

See Also:



193
194
195
# File 'lib/command_kit/options/option.rb', line 193

def default_value
  @value && @value.default_value
end

#descString+

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

If #default_value returns a value, the description will contain the Default: value the option will be initialized with.

The option description.

Returns:

  • (String, Array<String>)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/command_kit/options/option.rb', line 206

def desc
  if (value = default_value)
    default_text = "(Default: #{value})"

    case @desc
    when Array
      @desc + [default_text]
    else
      "#{@desc} #{default_text}"
    end
  else
    @desc
  end
end

#equals?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Specifies if the option is of the form --opt=VALUE.

Returns:

  • (Boolean)


135
136
137
# File 'lib/command_kit/options/option.rb', line 135

def equals?
  @equals
end

#separator'=', ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The separator character between the option and option value.

Returns:

  • ('=', ' ', nil)


144
145
146
147
148
149
150
# File 'lib/command_kit/options/option.rb', line 144

def separator
  if @value
    if equals? then '='
    else            ' '
    end
  end
end

#typeClass?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option value's type.

Returns:

  • (Class, nil)

See Also:



182
183
184
# File 'lib/command_kit/options/option.rb', line 182

def type
  @value && @value.type
end

#usageArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Usage strings for the option.

Returns:

  • (Array<String>)

    The usage strings.



158
159
160
161
162
163
164
# File 'lib/command_kit/options/option.rb', line 158

def usage
  if equals? && (@value && @value.optional?)
    [*@short, "#{@long}[#{separator}#{@value && @value.usage}]"]
  else
    [*@short, "#{@long}#{separator}#{@value && @value.usage}"]
  end
end

#value?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if the option has a value.

Returns:

  • (Boolean)


171
172
173
# File 'lib/command_kit/options/option.rb', line 171

def value?
  !@value.nil?
end