Sha256: b57d73104f3f29871582bf36b1f1847b3bf5d394e32daa5333bbd8789270b33d

Contents?: true

Size: 1.12 KB

Versions: 16

Compression:

Stored size: 1.12 KB

Contents

module SOPT
  def self.name(info)
    (info[:long] || info[:short]).sub(/^-*/,'')
  end

  def self.parse(opts)
    info = {}
    opts.split(/:/).each do |opt|
      short, long = opt.sub(/\*$/,'').split('--').values_at(0,1)
      i= {
        :arg => !!opt.match(/\*$/),
      }
      i[:short] = short unless short.nil? || short.empty?
      i[:long] = '--' + long unless long.nil? || long.empty?
      info[name(i)] = i
    end

    info
  end

  def self.get(opts)
    info = parse(opts)

    switches = {}
    info.each do |name, i|
      switches[i[:short]] = name if i[:short]
      switches[i[:long]] = name if i[:long]
    end

    options = Hash.new(false)
    rest = []

    index = 0
    while index < ARGV.length do
      arg = ARGV[index]
      if switches.include? arg
        name = switches[arg]
        i = info[name]
        if i[:arg]
          options[name.to_sym] = ARGV[index + 1]
          index += 1
        else
          options[name.to_sym] = true
        end
      else
        rest << arg
      end
      index += 1
    end

    ARGV.delete_if do true end
    rest.each do |e| ARGV << e end

    options
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rbbt-util-5.2.0 lib/rbbt/util/simpleopt.rb
rbbt-util-5.1.0 lib/rbbt/util/simpleopt.rb
rbbt-util-5.0.1 lib/rbbt/util/simpleopt.rb
rbbt-util-5.0.0 lib/rbbt/util/simpleopt.rb
rbbt-util-4.4.0 lib/rbbt/util/simpleopt.rb
rbbt-util-4.3.0 lib/rbbt/util/simpleopt.rb
rbbt-util-4.2.0 lib/rbbt/util/simpleopt.rb
rbbt-util-4.1.0 lib/rbbt/util/simpleopt.rb
rbbt-util-4.0.2 lib/rbbt/util/simpleopt.rb
rbbt-util-4.0.1 lib/rbbt/util/simpleopt.rb
rbbt-util-4.0.0 lib/rbbt/util/simpleopt.rb
rbbt-util-3.2.1 lib/rbbt/util/simpleopt.rb
rbbt-util-3.2.0 lib/rbbt/util/simpleopt.rb
rbbt-util-3.1.0 lib/rbbt/util/simpleopt.rb
rbbt-util-3.0.3 lib/rbbt/util/simpleopt.rb
rbbt-util-3.0.2 lib/rbbt/util/simpleopt.rb