Class: Cuprum::Cli::Option

Inherits:
Data
  • Object
show all
Defined in:
lib/cuprum/cli/option.rb

Overview

Data object representing a command option.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, aliases: [], default: nil, description: nil, parameter_name: nil, required: false, type: :string, variadic: false) ⇒ Option

Returns a new instance of Option.

Parameters:

  • name (String, Symbol)

    the name of the option.

  • aliases (Array<String, Symbol>) (defaults to: [])

    aliases for the option when parsing options from the command line.

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

    the default value for the option. If given and the value of the option is nil, sets the option value to the default value.

  • description (String) (defaults to: nil)

    a short, human-readable description of the option.

  • parameter_name (String) (defaults to: nil)

    a representation of the possible values for the option.

  • required (true, false) (defaults to: false)

    if true, raises an exception if the option is not provided to the command.

  • type (Class, String, Symbol) (defaults to: :string)

    the expected type of the option value as a Class or class name. If given, raises an exception if the option value is not an instance of the type. Defaults to :string.

  • variadic (true, false) (defaults to: false)

    if true, the option is variadic and represents an hash of options provided to the command. Defaults to false.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cuprum/cli/option.rb', line 36

def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  name:,
  aliases:        [],
  default:        nil,
  description:    nil,
  parameter_name: nil,
  required:       false,
  type:           :string,
  variadic:       false
)
  name     = name.to_sym
  aliases  = Array(aliases).compact.map { |obj| obj.to_s.tr('_', '-') }
  required = required ? true : false
  type     = type.to_sym if type.is_a?(String)
  variadic = variadic ? true : false

  super(
    aliases:,
    default:,
    description:,
    name:,
    parameter_name:,
    required:,
    type:,
    variadic:
  )
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases

Returns:

  • (Object)

    the current value of aliases



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def aliases
  @aliases
end

#defaultObject (readonly)

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def name
  @name
end

#parameter_nameObject (readonly)

Returns the value of attribute parameter_name

Returns:

  • (Object)

    the current value of parameter_name



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def parameter_name
  @parameter_name
end

#requiredObject (readonly) Also known as: required?

Returns the value of attribute required

Returns:

  • (Object)

    the current value of required



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def type
  @type
end

#variadicObject (readonly) Also known as: variadic?

Returns the value of attribute variadic

Returns:

  • (Object)

    the current value of variadic



8
9
10
# File 'lib/cuprum/cli/option.rb', line 8

def variadic
  @variadic
end

Instance Method Details

#resolve(value) ⇒ Object

Validates the value for the current option.

If the value is nil, applies the option default (if any).

Parameters:

  • value (Object)

    the value to validate.

Returns:

  • (Object)

    the validated option value.

Raises:

Raises:



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cuprum/cli/option.rb', line 79

def resolve(original_value)
  value = original_value
  value = default_value if blank?(value)
  value = value.to_s    if value.is_a?(Symbol)

  return default_value_for_type if value.nil? && !required?
  return value                  if valid_option?(value)

  raise Cuprum::Cli::Options::InvalidOptionError,
    invalid_option_message(original_value)
end