Class: Consoler::Option
- Inherits:
-
Object
- Object
- Consoler::Option
- Defined in:
- lib/consoler/option.rb
Overview
Represents an option
Instance Attribute Summary collapse
-
#aliases ⇒ Array
readonly
List of aliases of option (
-v|--verbose
). -
#is_argument ⇒ Boolean
readonly
Is the option an argument.
-
#is_long ⇒ Boolean
readonly
Is the option long (
--option
). -
#is_optional ⇒ Integer
readonly
Is the option optional (> 0) (
[option]
). -
#is_short ⇒ Boolean
readonly
Is the option short (
-o
). -
#is_value ⇒ Boolean
readonly
Does the option need a value (
--option=
). -
#name ⇒ String
readonly
Name of the options.
Class Method Summary collapse
-
.create(option_def, tracker) ⇒ Object
Create a option.
Instance Method Summary collapse
-
#_aliases(option, alias_defs, tracker) ⇒ (String, Array)
private
Parse all possible aliases.
-
#_is_long(option) ⇒ (String, Boolean)
private
Check long definition.
-
#_is_optional(option, tracker) ⇒ (String, Integer|nil)
private
Check optional definition.
-
#_is_short(option) ⇒ (String, Boolean)
private
Check short definition.
-
#_value(option, argument) ⇒ (String, Boolean)
private
Check value definition.
-
#default_value ⇒ nil | 0 | false
Get the default value of this option.
-
#initialize(option_def, tracker) ⇒ Option
constructor
protected
Create a option.
-
#to_definition ⇒ String
Get the definition of the option.
Constructor Details
#initialize(option_def, tracker) ⇒ Option (protected)
Create a option
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/consoler/option.rb', line 109 def initialize(option_def, tracker) # Check for multiple attributes in the option definition till we got the # final name and all of its attributes # make sure we don't wrongly process any alias alias_defs = option_def.split '|' option = alias_defs.shift || '' option, @is_optional = _is_optional option, tracker option, @is_long = _is_long option option, @is_short = _is_short option @is_argument = (!@is_long && !@is_short) option, @is_value = _value option, @is_argument option, @aliases = _aliases option, alias_defs, tracker if option[0] == '<' raise 'Invalid <, missing >' if option[-1] != '>' raise 'Only arguments support <, > around name' unless @is_argument option = option[1..-2] end raise 'Missing starting <' if option[-1] == '>' @name = option if @name.empty? raise 'Option must have a name' end if @is_long && @is_short raise 'Option can not be a long and a short option' end end |
Instance Attribute Details
#aliases ⇒ Array (readonly)
List of aliases of option (-v|--verbose
)
13 14 15 |
# File 'lib/consoler/option.rb', line 13 def aliases @aliases end |
#is_argument ⇒ Boolean (readonly)
Is the option an argument
13 14 15 |
# File 'lib/consoler/option.rb', line 13 def is_argument @is_argument end |
#is_long ⇒ Boolean (readonly)
Is the option long (--option
)
13 14 15 |
# File 'lib/consoler/option.rb', line 13 def is_long @is_long end |
#is_optional ⇒ Integer (readonly)
Is the option optional (> 0) ([option]
)
13 14 15 |
# File 'lib/consoler/option.rb', line 13 def is_optional @is_optional end |
#is_short ⇒ Boolean (readonly)
Is the option short (-o
)
13 14 15 |
# File 'lib/consoler/option.rb', line 13 def is_short @is_short end |
#is_value ⇒ Boolean (readonly)
Does the option need a value (--option=
)
13 14 15 |
# File 'lib/consoler/option.rb', line 13 def is_value @is_value end |
#name ⇒ String (readonly)
Name of the options
13 14 15 |
# File 'lib/consoler/option.rb', line 13 def name @name end |
Class Method Details
.create(option_def, tracker) ⇒ Object
Create a option
Yields an option for every option detected
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/consoler/option.rb', line 28 def self.create(option_def, tracker) option = Option.new option_def, tracker # split short options with more than 1 char in multiple options if option.is_short && option.name.size > 1 # remember state old_tracking = tracker.is_tracking old_is_value = option.is_value # if the complete option is optional, fake the tracker if option.is_optional tracker.is_tracking = true end names = option.name.split('') names.each_with_index do |name, i| new_name = "-#{name}" # if the short option should have a value, this only counts for the last option if old_is_value && i == names.count - 1 new_name = "#{new_name}=" end yield Option.new new_name, tracker end # reset to saved state tracker.is_tracking = old_tracking else yield option end end |
Instance Method Details
#_aliases(option, alias_defs, tracker) ⇒ (String, Array) (private)
Parse all possible aliases
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/consoler/option.rb', line 244 def _aliases(option, alias_defs, tracker) return option, [] if alias_defs.empty? raise 'Argument can\'t have aliases' if is_argument raise 'Aliases are not allowed for multiple short options' if is_short && option.size > 1 aliases_ = [] alias_names = [] while (alias_def = alias_defs.shift) Consoler::Option.create alias_def, tracker do |alias_| raise "Duplicate alias name: #{alias_.name}" if alias_names.include? alias_.name raise "Alias must have a value: #{alias_.name}" if is_value && !alias_.is_value raise "Alias can't have a value: #{alias_.name}" if !is_value && alias_.is_value aliases_.push alias_ alias_names.push alias_.name end end [option, aliases_] end |
#_is_long(option) ⇒ (String, Boolean) (private)
Check long definition
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/consoler/option.rb', line 192 def _is_long(option) if option[0..1] == '--' long = true option = option[2..-1] else long = false end [option, long] end |
#_is_optional(option, tracker) ⇒ (String, Integer|nil) (private)
Check optional definition
Does it open an optional group Does it close an optional group (can be both) Updates the tracker Removes leading [ and trailing ]
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/consoler/option.rb', line 158 def _is_optional(option, tracker) if option[0] == '[' if !tracker.is_tracking # mark tracker as tracking tracker.is_tracking = true tracker.index += 1 option = option[1..-1] else raise 'Nested optionals are not allowed' end end # get optional group index from tracking, if tracking optional = if tracker.is_tracking tracker.index end if option[-1] == ']' if tracker.is_tracking # mark tracker as non-tracking tracker.is_tracking = false option = option[0..-2] else raise 'Unopened optional' end end [option, optional] end |
#_is_short(option) ⇒ (String, Boolean) (private)
Check short definition
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/consoler/option.rb', line 207 def _is_short(option) if option[0] == '-' short = true option = option[1..-1] else short = false end [option, short] end |
#_value(option, argument) ⇒ (String, Boolean) (private)
Check value definition
223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/consoler/option.rb', line 223 def _value(option, argument) if option[-1] == '=' if argument raise 'Arguments can\'t have a value' end value = true option = option[0..-2] else value = false end [option, value] end |
#default_value ⇒ nil | 0 | false
Get the default value of this option
93 94 95 96 97 98 99 |
# File 'lib/consoler/option.rb', line 93 def default_value return nil if is_value return 0 if is_short return false if is_long nil end |
#to_definition ⇒ String
Get the definition of the option
Does not include the optional information, as that is linked to other options
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/consoler/option.rb', line 68 def to_definition definition = name if is_long definition = "--#{definition}" elsif is_short definition = "-#{definition}" end if is_value definition = "#{definition}=" elsif is_argument definition = "<#{definition}>" end aliases.each do |alias_| definition = "#{definition}|#{alias_.to_definition}" end definition end |