Class: CommandMapper::Option
- Inherits:
-
Object
- Object
- CommandMapper::Option
- Defined in:
- lib/command_mapper/option.rb
Overview
Represents an option for a command.
Instance Attribute Summary collapse
-
#flag ⇒ String
readonly
The option's flag (ex:
-o
or--output
). -
#name ⇒ Symbol
readonly
The option's name.
-
#value ⇒ OptionValue?
readonly
Describes the option's value.
Class Method Summary collapse
-
.infer_name_from_flag(flag) ⇒ Symbol
private
Infers a method name from the given option flag.
Instance Method Summary collapse
-
#accepts_value? ⇒ Boolean
Indicates whether the option accepts a value.
-
#argv(argv = [], value) ⇒ Array<String>
Converts the given value into the command-line arguments for the option's flag and value.
-
#equals? ⇒ Boolean
Indicates whether the option flag and value should be separated with a
=
character. -
#initialize(flag, name: nil, value: nil, repeats: false, equals: nil, value_in_flag: nil) ⇒ Option
constructor
private
Initializes the option.
-
#repeats? ⇒ Boolean
Determines whether the option can be given multiple times.
-
#validate(value) ⇒ true, (false, String)
Validates whether the given value is compatible with the option.
-
#value_in_flag? ⇒ Boolean
Indicates whether the value will be appended to the option's flag.
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.
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
#flag ⇒ String (readonly)
The option's flag (ex: -o
or --output
).
13 14 15 |
# File 'lib/command_mapper/option.rb', line 13 def flag @flag end |
#name ⇒ Symbol (readonly)
The option's name.
18 19 20 |
# File 'lib/command_mapper/option.rb', line 18 def name @name end |
#value ⇒ OptionValue? (readonly)
Describes the option's value.
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.
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.
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.
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, = validate(value) unless valid raise(ValidationError,"option #{@name} was given an invalid value (#{value.inspect}): #{}") 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.
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.
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.
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.
139 140 141 |
# File 'lib/command_mapper/option.rb', line 139 def value_in_flag? @value_in_flag end |