Class: Cuprum::Cli::Argument

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

Overview

Data object representing a positional command argument.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, default: nil, description: nil, parameter_name: nil, required: false, type: :string, variadic: false) ⇒ Argument

Returns a new instance of Argument.

Parameters:

  • name (String, Symbol)

    the name of the argument.

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

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

  • description (String) (defaults to: nil)

    a short, human-readable description of the argument.

  • parameter_name (String) (defaults to: nil)

    a representation of the possible values for the argument.

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

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

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

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

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

    if true, the argument is variadic and represents an array of arguments provided to the command. Defaults to false.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cuprum/cli/argument.rb', line 33

def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  name:,
  default:        nil,
  description:    nil,
  parameter_name: nil,
  required:       false,
  type:           :string,
  variadic:       false
)
  name     = name.to_sym
  required = required ? true : false
  type     = type.to_sym if type.is_a?(String)
  variadic = variadic ? true : false

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

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



8
9
10
# File 'lib/cuprum/cli/argument.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/argument.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/argument.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/argument.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/argument.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/argument.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/argument.rb', line 8

def variadic
  @variadic
end

Instance Method Details

#resolve(value) ⇒ Object

Validates the value for the current argument.

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

Parameters:

  • value (Object)

    the value to validate.

Returns:

  • (Object)

    the validated argument value.

Raises:

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cuprum/cli/argument.rb', line 73

def resolve(original_value) # rubocop:disable Metrics/CyclomaticComplexity
  return resolve_variadic(original_value) if variadic?

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

  return (type == :boolean ? false : nil) if value.nil? && !required?

  return value if valid_argument?(value)

  raise Cuprum::Cli::Arguments::InvalidArgumentError,
    invalid_argument_message(original_value)
end