Sha256: e64a51b1b29e87a210357be6779d796b8c1073732f2e9a9a7bab2a5408489b83
Contents?: true
Size: 1.02 KB
Versions: 1
Compression:
Stored size: 1.02 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.gsub('_', '-') if str.start_with?('--') } 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.19 | lib/cl/parser.rb |