Class: Consoler::Options

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

Overview

List of options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options_def) ⇒ Options

Create a list of option based on a string definition

Parameters:

  • options_def (String)

    A string definition of the desired options

Raises:

  • (RuntimeError)

    if you try to use a duplicate name



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/consoler/options.rb', line 17

def initialize(options_def)
  @options = []
  @description = nil

  return if options_def.nil?

  # strip the description
  if match = /(^|\s+)-- (?<description>.*)$/.match(options_def) then
    @description = match[:description]
    options_def = options_def[0...-match[0].size]
  end

  options = options_def.split ' '
  tracker = Consoler::OptionalsTracker.new

  option_names = []

  while option_def = options.shift do
    Consoler::Option.create option_def, tracker do |option|
      raise "Duplicate option name: #{option.name}" if option_names.include? option.name

      @options.push option
      option_names.push option.name
    end
  end
end

Instance Attribute Details

#descriptionString (readonly)

Description of the options

Returns:

  • (String)

    the current value of description



10
11
12
# File 'lib/consoler/options.rb', line 10

def description
  @description
end

Instance Method Details

#each {|Consoler::Option, Integer| ... } ⇒ Consoler::Options

Loop through all options

Yields:

Returns:



62
63
64
65
66
67
68
# File 'lib/consoler/options.rb', line 62

def each
  @options.each_with_index do |option, i|
    yield option, i
  end

  self
end

#get(name) ⇒ Consoler::Option?

Get a options by its name

Parameters:

  • name (String)

    Name of the option

Returns:



48
49
50
51
52
53
54
55
56
# File 'lib/consoler/options.rb', line 48

def get(name)
  each do |option, _|
    if option.name == name then
      return option
    end
  end

  return nil
end

#sizeInteger

Get the number of options

Returns:

  • (Integer)


73
74
75
# File 'lib/consoler/options.rb', line 73

def size
  @options.size
end

#to_definitionString

Get the definition for these options

Returns:

  • (String)

    Options definition



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/consoler/options.rb', line 80

def to_definition
  definition = ''
  optional = nil

  each do |option, i|
    definition += ' '

    if optional.nil? and option.is_optional then
      definition += '['
      optional = option.is_optional
    end

    definition += option.to_definition

    if option.is_optional then
      # only close when the next option is not optional, or another optional group
      if @options[i + 1].nil? or optional != @options[i + 1].is_optional then
        definition += ']'
        optional = nil
      end
    end
  end

  definition.strip
end