我有一个解析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