Class: Balmora::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/balmora/arguments.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

._wrap(text, length) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/balmora/arguments.rb', line 162

def self._wrap(text, length)
  line = ''
  result = []

  text.split("\n").each() { |text_line|
    text_line.split(' ').each() { |word|
      if (line + word + ' ').length > length
        result.push(line.rstrip())
        line = ''
      end

      line += word + ' '
    }

    result.push(line.rstrip())
    line = ''
  }

  return result
end

.format(lines) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/balmora/arguments.rb', line 127

def self.format(lines)
  indent =
    lines.
    inject(0) { |current, line|
      if line.instance_of?(::String)
        next current
      end

      if current < line[0].length
        current = line[0].length
      end

      current
    } +
    4

  result = []
  lines.each() { |line|
    if line.instance_of?(::String)
      result.push(line)
      next
    end

    summary = _wrap(line[1] || '', 60)
    result.push(line[0] + (' ' * (indent - line[0].length)) +
      (summary[0] || ''))

    (summary[1..-1] || []).each() { |ln| result.push(' ' * indent + ln) }

    result.push('')
  }

  return result.join("\n").rstrip()
end

.help(options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/balmora/arguments.rb', line 106

def self.help(options)
  lines = []
  options.each() { |long, option|
    if option.instance_of?(::String)
      lines.push(option)
      next
    end

    arg =
      if option[:shortcut].nil?()
        (' ' * 7) + '--' + long.to_s()
      else
        (' ' * 4) + '-' + option[:shortcut] + ', ' + '--' + long.to_s()
      end

    lines.push([arg, option[:description]])
  }

  return format(lines)
end

.parse(options, argv) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/balmora/arguments.rb', line 5

def self.parse(options, argv)
  shortcuts = _shortcuts(options)

  result = {}
  index = 0
  tail = nil
  while index < argv.length
    arg = argv[index]

    if arg.start_with?('--')
      new_index = _parse_key(result, options, argv, index, arg[2..-1])
    elsif arg.start_with?('-')
      new_index = _parse_flags(result, options, argv, index, shortcuts, arg)
    else
      new_index = nil
    end

    if new_index.nil?()
      tail = argv[index..-1]
      break
    end

    index = new_index + 1
  end

  options.each() { |key, value|
    if value[:flag] && !result.has_key?(key)
      result[key] = false
    end
  }

  return result, tail
end