Class: CommandMapper::Argument

Inherits:
Arg
  • Object
show all
Defined in:
lib/command_mapper/argument.rb

Overview

Represents an additional argument of a command.

Instance Attribute Summary collapse

Attributes inherited from Arg

#type

Instance Method Summary collapse

Methods inherited from Arg

#optional?, #required?

Constructor Details

#initialize(name, required: true, type: Types::Str.new, repeats: false) ⇒ Argument

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

Parameters:

  • name (Symbol)

    The argument's name.

  • required (Boolean) (defaults to: true)

    Specifies whether the argument is required or can be omitted.

  • type (Types::Type, Hash) (defaults to: Types::Str.new)

    The value type of the argument.

  • repeats (Boolean) (defaults to: false)

    Specifies whether the argument can be given multiple times.

Raises:

  • (ArgumentError)

    The given type: must not be false or nil.



35
36
37
38
39
40
# File 'lib/command_mapper/argument.rb', line 35

def initialize(name, required: true, type: Types::Str.new, repeats: false)
  super(required: required, type: type)

  @name    = name
  @repeats = repeats
end

Instance Attribute Details

#nameSymbol (readonly)

The argument name.

Returns:

  • (Symbol)


13
14
15
# File 'lib/command_mapper/argument.rb', line 13

def name
  @name
end

Instance Method Details

#argv(argv = [], value) ⇒ Array<String>

Converts the given value into the command-line arguments for the argument's flag and value.

Parameters:

  • argv (Array) (defaults to: [])

    The argv array.

  • value (Object)

    The value for the argument.

Returns:

  • (Array<String>)

    The command-line arguments.

Raises:

  • (ArgumentError)

    The given value was incompatible with the argument.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/command_mapper/argument.rb', line 110

def argv(argv=[],value)
  valid, message = validate(value)

  unless valid
    raise(ValidationError,"argument #{@name} was given an invalid value (#{value.inspect}): #{message}")
  end

  if repeats?
    values = Array(value)

    values.each do |element|
      emit_arg_value(argv,element)
    end
  else
    emit_arg_value(argv,value)
  end

  return argv
end

#repeats?Boolean

Indicates whether the arg can be repeated multiple times or not.

Returns:

  • (Boolean)


47
48
49
# File 'lib/command_mapper/argument.rb', line 47

def repeats?
  @repeats
end

#validate(value) ⇒ true, (false, String)

Validates whether a given value is compatible with the arg.

Parameters:

  • value (Array<Object>, Object)

    The given value to validate.

Returns:

  • (true, (false, String))

    Returns true if the value is valid, or false and a validation error message if the value is not compatible.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/command_mapper/argument.rb', line 63

def validate(value)
  if repeats?
    values = case value
             when Array then value
             else            [value]
             end

    if required?
      # argument requires atleast one value
      if values.empty?
        return [false, "requires at least one value"]
      end
    end

    # validate each element in the value
    values.each do |element|
      valid, message = @type.validate(element)

      unless valid
        return [valid, message]
      end
    end

    return true
  else
    super(value)
  end
end