Class: CommandKit::Options::OptionValue Private

Inherits:
Arguments::ArgumentValue show all
Defined in:
lib/command_kit/options/option_value.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 an additional argument associated with an option flag.

Constant Summary collapse

DEFAULT_USAGES =

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

Maps OptionParser types to USAGE strings.

{
  # NOTE: NilClass and Object are intentionally omitted
  Date       => 'DATE',
  DateTime   => 'DATE_TIME',
  Time       => 'TIME',
  URI        => 'URI',
  Shellwords => 'STR',
  String     => 'STR',
  Integer    => 'INT',
  Float      => 'DEC',
  Numeric    => 'NUM',
  OptionParser::DecimalInteger => 'INT',
  OptionParser::OctalInteger   => 'OCT',
  OptionParser::DecimalNumeric => 'NUM|DEC',
  TrueClass  => 'BOOL',
  FalseClass => 'BOOL',
  Array      => 'LIST[,...]',
  Regexp     => '/REGEXP/'
}

Instance Attribute Summary collapse

Attributes inherited from Arguments::ArgumentValue

#required

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Arguments::ArgumentValue

#optional?, #required?

Constructor Details

#initialize(type: String, default: nil, usage: self.class.default_usage(type), **kwargs) ⇒ OptionValue

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 value.

Parameters:

  • type (Class, Hash, Array, Regexp) (defaults to: String)

    The type of the option value.

  • default (Object, Proc, nil) (defaults to: nil)

    The default parsed value for the option value.

  • usage (String, nil) (defaults to: self.class.default_usage(type))

    The optional usage string for the option value.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • required (Boolean)

    Specifies whether the option value is required or optional.



70
71
72
73
74
75
76
77
78
# 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

Instance Attribute Details

#defaultObject, ... (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 default parsed value for the argument value.

Returns:

  • (Object, Proc, nil)


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

def default
  @default
end

#typeClass, ... (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 desired type of the argument value.

Returns:

  • (Class, Hash, Array, Regexp)


45
46
47
# File 'lib/command_kit/options/option_value.rb', line 45

def type
  @type
end

Class Method Details

.default_usage(type) ⇒ 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.

Returns the default option value usage for the given type.

Parameters:

  • type (Class, Hash, Array, Regexp)

    The option value type.

Returns:

  • (String, nil)

    A default usage string based on the option value type.

Raises:

  • (TypeError)

    The given type was not a Class, Hash, Array, or Regexp.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/command_kit/options/option_value.rb', line 92

def self.default_usage(type)
  DEFAULT_USAGES.fetch(type) do
    case type
    when Hash   then type.keys.join('|')
    when Array  then type.join('|')
    when Regexp then type.source
    when Class
      Inflector.underscore(Inflector.demodularize(type.name)).upcase
    else
      raise(TypeError,"unsupported option type: #{type.inspect}")
    end
  end
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.

Returns a new default value.

Returns:

  • (Object)


122
123
124
125
126
# File 'lib/command_kit/options/option_value.rb', line 122

def default_value
  if @default.respond_to?(:call) then @default.call
  else                                @default.dup
  end
end

#usageString?

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 usage string for the argument.

Returns:

  • (String, nil)


111
112
113
114
115
# File 'lib/command_kit/options/option_value.rb', line 111

def usage
  string = @usage
  string = "[#{string}]" if optional?
  string
end