我有一个选项解析 gem,它提供了用于创建命令的 DSL。在实践中,它通常最终看起来像这样:
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
这些是类方法。如您所见,不是很干燥。写这样的东西会更好:
scope arity: [1, -1], on_multiple: :append do
option :ints, Integer
option :floats, Float
option :complex, Complex
end
将给定的选项散列scope
与给定的散列透明地合并到option
. 这就是我卡住的地方。我不确定在哪里存储常用选项,以便以后合并它们。
有任何想法吗?
option
将所有内容转发到Option#new
:
def option(*args, &block)
# self is a class that represents a command
self.options << Option.new(*args, &block)
end
根据要求,这是代码,删除了支持宝石的使用:
def initialize(key, *args, &block)
# Retrieve the options hash from the argument array.
options = args.last.is_a?(Hash) ? args.pop : {}
# The rest of the implementation...
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