Sha256: 15a648396e40dfc5a0487ffd630f5afb7316ef398d4846cf9b0cc60c7c9dc847

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'optparse'

class Cl
  class Parser < OptionParser
    attr_reader :opts

    def initialize(opts, args)
      @opts = {}

      super do
        opts.each do |opt|
          on(*dasherize(opt.strs)) do |value|
            set(opt, value)
          end

          opt.aliases.each do |name|
            on(aliased(opt, name)) do |value|
              @opts[name] = set(opt, value)
            end
          end
        end
      end

      dasherize!(args)
      parse!(args)
    end

    def aliased(opt, name)
      str = opt.strs.detect { |str| str.start_with?('--') } || raise
      str.sub(opt.name.to_s, name.to_s)
    end

    # should consider negative arities (e.g. |one, *two|)
    def set(opt, value)
      args = [opts, opt.type, opt.name, value]
      args = args[-opt.block.arity, opt.block.arity]
      instance_exec(*args, &opt.block)
    end

    def dasherize(strs)
      strs.map { |str| str.start_with?('--') ? str.gsub('_', '-') : str }
    end

    def dasherize!(strs)
      strs.replace(dasherize(strs))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cl-0.1.20 lib/cl/parser.rb