Sha256: c1a55b23003a60be19c47baa04fb68496ea68b1d25e74df5fbfcc6ab3d1eca09

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

require 'optparse'

class Cl
  class Parser < OptionParser
    attr_reader :opts

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

      super do
        opts.each do |opt|
          on(*underscore!(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

      underscore!(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

    # OptionParser has started accepting dasherized options in 2.4.
    # We want to support them on any Ruby >= 2.0 version, so we'll
    # need to normalize things ourselves.

    PATTERN = /^(-{1,2})(\[?no-\]?)?(.*)$/

    def underscore!(strs)
      return strs if RUBY_VERSION >= '2.4'
      strs.each { |str| str.gsub!(PATTERN) { "#{$1}#{$2}#{$3.tr('-', '_')}" } }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
cl-0.1.16 lib/cl/parser.rb
cl-0.1.15 lib/cl/parser.rb
cl-0.1.14 lib/cl/parser.rb
cl-0.1.13 lib/cl/parser.rb
cl-0.1.12 lib/cl/parser.rb
cl-0.1.11 lib/cl/parser.rb
cl-0.1.10 lib/cl/parser.rb
cl-0.1.9 lib/cl/parser.rb
cl-0.1.8 lib/cl/parser.rb