Sha256: 316ba8bbc5a5c7b6ca329f46972912524d014a90c5049841be06e938b62efcd1

Contents?: true

Size: 1.62 KB

Versions: 40

Compression:

Stored size: 1.62 KB

Contents

require 'rbbt/util/simpleopt/accessor'

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

    current = shortcuts.select{|s,l| l == long}.collect{|s,l| s }.first
    return current if current

    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 = []
    if opt_str.include? "\n"
      re = /\n+/
    else
      re = /:/
    end
    opt_str.split(re).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

40 entries across 40 versions & 1 rubygems

Version Path
rbbt-util-5.22.3 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.22.1 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.22.0 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.137 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.136 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.135 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.134 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.133 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.132 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.131 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.130 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.129 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.128 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.127 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.126 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.125 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.123 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.122 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.121 lib/rbbt/util/simpleopt/parse.rb
rbbt-util-5.21.120 lib/rbbt/util/simpleopt/parse.rb