I have a parsing gem option that provides DSL for creating commands. In practice, it often looks like this:
option :ints, Integer, arity: [1, -1], on_multiple: :append
option :floats, Float, arity: [1, -1], on_multiple: :append
option :complex, Complex, arity: [1, -1], on_multiple: :append
These are class methods. Not very DRY, as you can see. It would be better to write something like this:
scope arity: [1, -1], on_multiple: :append do
option :ints, Integer
option :floats, Float
option :complex, Complex
end
So that the hash parameters provided scopeare transparently merged with the parameter specified in option. This is where I am stuck. I am not sure where to store the general parameters so that I can combine them later.
Any ideas?
optionredirects everything to Option#new:
def option(*args, &block)
self.options << Option.new(*args, &block)
end
As requested here, the code using the supported stones has been removed:
def initialize(key, *args, &block)
options = args.last.is_a?(Hash) ? args.pop : {}
type = args.find { |arg| arg.is_a? Module }
strings = args.flatten.select do |arg|
arg.is_a? String
end.group_by do |arg|
arg =~ Parser::Regexp::SWITCH ? :switches : :description
end
self.key = key
self.names = strings.fetch(:switches) { [ Option.name_from(key) ] }
self.description = options.fetch :description, strings.fetch(:description, []).first
self.on_multiple = options.fetch :on_multiple, :replace
self.arity = options.fetch :arity, nil
self.default = options.fetch :default, nil
self.required = options.fetch :required, false
self.type = type || String
self.handler = block
end
Original on GitHub .