class Balmora::Arguments

Public Class Methods

_wrap(text, length) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Protected Class Methods

_parse_flags(result, options, argv, index, shortcuts, arg) click to toggle source
# File lib/balmora/arguments.rb, line 57
def self._parse_flags(result, options, argv, index, shortcuts, arg)
  new_index = nil

  if arg.length > 2
    arg[1..-1].split('').each() { |flag|
      new_index = _parse_key(result, options, argv, index,
        shortcuts[flag.to_sym()], true)

      if new_index.nil?()
        return nil
      end
    }
  else
    new_index = _parse_key(result, options, argv, index,
      shortcuts[arg[1..-1].to_sym()])
  end

  return new_index
end
_parse_key(result, options, argv, index, key, flag_required = false) click to toggle source
# File lib/balmora/arguments.rb, line 77
def self._parse_key(result, options, argv, index, key, flag_required = false)
  if key.nil?()
    return nil
  end

  key = key.to_sym()

  if !options.has_key?(key)
    return nil
  end

  option = options[key]
  if option[:flag] == true
    result[key] = true
  else
    if flag_required
      return nil
    end

    value = argv[index + 1]
    result[key] = value
    index += 1
  end

  return index
end
_shortcuts(options) click to toggle source
# File lib/balmora/arguments.rb, line 41
def self._shortcuts(options)
  shortcuts = Hash[
    options.
      collect() { |key, option|
        if !option.has_key?(:shortcut)
          next nil
        end

        [option[:shortcut].to_sym(), key]
      }.
      compact()
  ]

  return shortcuts
end