Class: Consoler::Option

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

Overview

Represents an option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_def, tracker) ⇒ Option (protected)

Create a option

Parameters:

  • option_def (String)

    Definition of the option

  • tracker (Consoler::Tracker)

    tracker

Raises:

  • (RuntimeError)

    if the option name is empty

  • (RuntimeError)

    if the option is long and short



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_argumentBoolean (readonly)

Is the option an argument

Returns:

  • (Boolean)

    the current value of is_argument



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

def is_argument
  @is_argument
end

#is_longBoolean (readonly)

Is the option long (--option)

Returns:

  • (Boolean)

    the current value of is_long



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

def is_long
  @is_long
end

#is_optionalInteger (readonly)

Is the option optional (> 0) ([option])

Returns:

  • (Integer)

    the current value of is_optional



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

def is_optional
  @is_optional
end

#is_shortBoolean (readonly)

Is the option short (-o)

Returns:

  • (Boolean)

    the current value of is_short



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

def is_short
  @is_short
end

#is_valueBoolean (readonly)

Does the option need a value (--option=)

Returns:

  • (Boolean)

    the current value of is_value



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

def is_value
  @is_value
end

#nameString (readonly)

Name of the options

Returns:

  • (String)

    the current value of name



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

Parameters:



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

Parameters:

  • option (String)

    Option definition

Returns:

  • ((String, Boolean))


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 ]

Parameters:

  • option (String)

    Option definition

  • tracker (Consoler::Tracker)

    Optional tracker

Returns:

  • ((String, Integer|nil))

    Remaining option definition, and, optional group if available

Raises:

  • (RuntimeError)

    if you try to nest optional groups

  • (RuntimeError)

    if you try to close an unopened optional



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

Parameters:

  • option (String)

    Option definition

Returns:

  • ((String, Boolean))


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

Parameters:

  • option (String)

    Option definition

Returns:

  • ((String, Boolean))

Raises:

  • (RuntimeError)

    if you try to assign a value to an argument



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_valuenil | 0 | false

Get the default value of this option

Returns:

  • (nil | 0 | false)


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_definitionString

Get the definition of the option

Does not include the optional information, as that is linked to other options

Returns:

  • (String)


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