Sha256: 68c0fe0a8711be20449514763dc6c7d71749ac23386ae5f96592de13e1120b6e

Contents?: true

Size: 1.45 KB

Versions: 220

Compression:

Stored size: 1.45 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 shortcuts[short].index current * ""
        next_letter = chars.shift
        return nil if next_letter.nil?
        current << next_letter
      end
      short = current * ""
    end

    return nil if shortcuts.include? short

    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

220 entries across 220 versions & 1 rubygems

Version Path
rbbt-util-5.19.11 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.10 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.9 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.8 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.7 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.6 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.5 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.4 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.3 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.2 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.1 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.19.0 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.18.1 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.18.0 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.17.89 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.17.88 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.17.87 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.17.86 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.17.85 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.17.84 lib/rbbt/util/simpleopt/parse.rb