Class: CommandMapper::Option

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

Overview

Represents an option for a command.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flag, name: nil, value: nil, repeats: false, equals: nil, value_in_flag: nil) ⇒ Option

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.

Parameters:

  • flag (String)

    The option's flag (ex: -o or --output).

  • name (Symbol, nil) (defaults to: nil)

    The option's name.

  • value (Hash, nil) (defaults to: nil)

    The option's value.

  • repeats (Boolean) (defaults to: false)

    Specifies whether the option can be given multiple times.

  • equals (Boolean) (defaults to: nil)

    Specifies whether the option's flag and value should be separated with a = character.

  • value_in_flag (Boolean) (defaults to: nil)

    Specifies that the value should be appended to the option's flag (ex: -Fvalue).

Options Hash (value:):

  • :required (Boolean)

    Specifies whether the option requires a value or not.

  • :type (Types:Type, Hash, nil)

    The explicit type for the option's value.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/command_mapper/option.rb', line 56

def initialize(flag, name: nil, value: nil, repeats: false,
                     # formatting options
                     equals:        nil,
                     value_in_flag: nil)
  @flag    = flag
  @name    = name || self.class.infer_name_from_flag(flag)
  @value   = case value
             when Hash then OptionValue.new(**value)
             when true then OptionValue.new
             end
  @repeats = repeats

  # formatting options
  @equals        = equals
  @value_in_flag = value_in_flag
end

Instance Attribute Details

#flagString (readonly)

The option's flag (ex: -o or --output).

Returns:

  • (String)


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

def flag
  @flag
end

#nameSymbol (readonly)

The option's name.

Returns:

  • (Symbol)


18
19
20
# File 'lib/command_mapper/option.rb', line 18

def name
  @name
end

#valueOptionValue? (readonly)

Describes the option's value.

Returns:



23
24
25
# File 'lib/command_mapper/option.rb', line 23

def value
  @value
end

Class Method Details

.infer_name_from_flag(flag) ⇒ Symbol

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.

Infers a method name from the given option flag.

Parameters:

  • flag (String)

    The given long or short option flag.

Returns:

  • (Symbol)

    The inferred method method name.

Raises:

  • (ArgumentError)

    Could not infer the name from the given option flag or was not given a valid option flag.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/command_mapper/option.rb', line 88

def self.infer_name_from_flag(flag)
  if flag.start_with?('--')
    name = flag[2..-1]
  elsif flag.start_with?('-')
    name = flag[1..-1]

    if name.length < 2
      raise(ArgumentError,"cannot infer a name from short option flag: #{flag.inspect}")
    end
  else
    raise(ArgumentError,"not an option flag: #{flag}")
  end

  name.downcase.gsub(/[_-]+/,'_').to_sym
end

Instance Method Details

#accepts_value?Boolean

Indicates whether the option accepts a value.

Returns:

  • (Boolean)


109
110
111
# File 'lib/command_mapper/option.rb', line 109

def accepts_value?
  !@value.nil?
end

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

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

Parameters:

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

    The argv array.

  • value (Object)

    The value given to the option.

Returns:

  • (Array<String>)

Raises:

  • (ArgumentError)

    The given value was incompatible with the option.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/command_mapper/option.rb', line 184

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

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

  if accepts_value?
    if repeats?
      values = Array(value)

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

  return argv
end

#equals?Boolean

Indicates whether the option flag and value should be separated with a = character.

Returns:

  • (Boolean)


128
129
130
# File 'lib/command_mapper/option.rb', line 128

def equals?
  @equals
end

#repeats?Boolean

Determines whether the option can be given multiple times.

Returns:

  • (Boolean)


118
119
120
# File 'lib/command_mapper/option.rb', line 118

def repeats?
  @repeats
end

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

Validates whether the given value is compatible with the option.

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.



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/command_mapper/option.rb', line 155

def validate(value)
  if accepts_value?
    if repeats?
      validate_repeating(value)
    else
      @value.validate(value)
    end
  else
    validate_does_not_accept_value(value)
  end
end

#value_in_flag?Boolean

Indicates whether the value will be appended to the option's flag.

Returns:

  • (Boolean)

Since:

  • 0.2.0



139
140
141
# File 'lib/command_mapper/option.rb', line 139

def value_in_flag?
  @value_in_flag
end