Class: Consoler::Option
- Inherits:
-
Object
- Object
- Consoler::Option
- Defined in:
- lib/consoler/option.rb
Overview
Represents an option
Instance Attribute Summary collapse
-
#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
-
#_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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/consoler/option.rb', line 102 def initialize(option_def, tracker) # Check for multiple attributes in the option definition till we got the # final name and all of its attributes option, @is_optional = _is_optional option_def, tracker option, @is_long = _is_long option option, @is_short = _is_short option @is_argument = (not @is_long and not @is_short) option, @is_value = _value option, @is_argument @name = option if @name.empty? then raise 'Option must have a name' end if @is_long and @is_short raise 'Option can not be a long and a short option' end end |
Instance Attribute Details
#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
27 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 |
# File 'lib/consoler/option.rb', line 27 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 and option.name.size > 1 then # 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 then 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 and i == names.count - 1 then 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
#_is_long(option) ⇒ (String, Boolean) (private)
Check long definition
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/consoler/option.rb', line 173 def _is_long(option) if option[0..1] == '--' then long = true option = option[2..-1] else long = false end return 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 ]
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/consoler/option.rb', line 137 def _is_optional(option, tracker) if option[0] == '[' then if !tracker.is_tracking then # 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 then tracker.index else nil end if option[-1] == ']' then if tracker.is_tracking then # mark tracker as non-tracking tracker.is_tracking = false option = option[0..-2] else raise 'Unopened optional' end end return option, optional end |
#_is_short(option) ⇒ (String, Boolean) (private)
Check short definition
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/consoler/option.rb', line 188 def _is_short(option) if option[0] == '-' then short = true option = option[1..-1] else short = false end return option, short end |
#_value(option, argument) ⇒ (String, Boolean) (private)
Check value definition
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/consoler/option.rb', line 204 def _value(option, argument) if option[-1] == '=' then if argument then raise 'Arguments can\'t have a value' end value = true option = option[0..-2] else value = false end return option, value end |
#default_value ⇒ nil | 0 | false
Get the default value of this option
86 87 88 89 90 91 92 |
# File 'lib/consoler/option.rb', line 86 def default_value return nil if is_value return 0 if is_short return false if is_long return nil end |
#to_definition ⇒ String
Get the definition of the option
Does not include the optional information, as that is linked to other options
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/consoler/option.rb', line 67 def to_definition definition = name if is_long then definition = "--#{definition}" elsif is_short then definition = "-#{definition}" end if is_value then definition = "#{definition}=" end definition end |