Class: CommandMapper::Argument
- Defined in:
- lib/command_mapper/argument.rb
Overview
Represents an additional argument of a command.
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
The argument name.
Attributes inherited from Arg
Instance Method Summary collapse
-
#argv(argv = [], value) ⇒ Array<String>
Converts the given value into the command-line arguments for the argument's flag and value.
-
#initialize(name, required: true, type: Types::Str.new, repeats: false) ⇒ Argument
constructor
private
Initializes the argument.
-
#repeats? ⇒ Boolean
Indicates whether the arg can be repeated multiple times or not.
-
#validate(value) ⇒ true, (false, String)
Validates whether a given value is compatible with the arg.
Methods inherited from Arg
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.
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
#name ⇒ Symbol (readonly)
The argument name.
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.
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, = validate(value) unless valid raise(ValidationError,"argument #{@name} was given an invalid value (#{value.inspect}): #{}") 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.
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.
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, = @type.validate(element) unless valid return [valid, ] end end return true else super(value) end end |