Sha256: 7af081f04ebbb5c2e25dfcb194e389f40f0a8d18071b558750617ae3907f881c

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'rbbt/util/simpleopt/accessor'

module SOPT
  def self.fix_shortcut(short, long)
    return short unless short and shortcuts.include?(short)

    chars = long.chars.to_a
    current = [chars.shift]
    short = current * ""

    if (shortcuts.include?(short) and not shortcuts[short] == long) and long.index "-" or long.index "_"
      parts = long.split(/[_-]/)
      acc = parts.collect{|s| s[0] } * ""
      return acc unless shortcuts.include? acc
    end

    while shortcuts.include?(short) and not shortcuts[short] == long
      while (long.length - current.length > 2) and shortcuts[short].index current * ""
        next_letter = chars.shift
        return nil if next_letter.nil?
        current << next_letter
      end
      short = current * ""
    end

    short
  end

  def self.register(short, long, asterisk, description)
    short = fix_shortcut(short, long)
    shortcuts[short] = long if short
    inputs << long
    input_shortcuts[long] = short
    input_descriptions[long] = description
    input_types[long] = asterisk ? :string : :boolean
  end

  def self.parse(opt_str)
    info = {}

    inputs = []
    opt_str.split(/[:\n]+/).each do |entry|
      entry.strip!
      next if entry.empty?
      names, _sep, description = entry.partition /\s+/
      short, long, asterisk = names.match(/\s*(?:-(.+))?(?:--(.+?))([*])?$/).values_at 1,2,3 

      inputs << long
      register short, long, asterisk, description
    end
    inputs
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rbbt-util-5.6.17 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.6.16 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.6.15 lib/rbbt/util/simpleopt/parse.rb