2013-01-30 11:04:54 -06:00
|
|
|
require 'set'
|
|
|
|
|
2013-01-23 00:26:23 -06:00
|
|
|
class Option
|
|
|
|
include Comparable
|
|
|
|
|
|
|
|
attr_reader :name, :description, :flag
|
|
|
|
|
|
|
|
def initialize(name, description=nil)
|
2013-01-23 00:26:28 -06:00
|
|
|
@name, @flag = split_name(name)
|
2013-01-23 00:26:23 -06:00
|
|
|
@description = description.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
flag
|
|
|
|
end
|
|
|
|
alias_method :to_str, :to_s
|
|
|
|
|
|
|
|
def to_json
|
|
|
|
flag.inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
def <=>(other)
|
|
|
|
name <=> other.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
other.is_a?(self.class) && hash == other.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
|
|
|
name.hash
|
|
|
|
end
|
2013-01-23 00:26:28 -06:00
|
|
|
|
2013-05-24 10:56:02 -05:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class}: #{flag.inspect}>"
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:28 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def split_name(name)
|
|
|
|
case name
|
2013-01-30 11:04:54 -06:00
|
|
|
when /^[a-zA-Z]$/
|
|
|
|
[name, "-#{name}"]
|
2013-01-23 00:26:28 -06:00
|
|
|
when /^-[a-zA-Z]$/
|
|
|
|
[name[1..1], name]
|
|
|
|
when /^--(.+)$/
|
|
|
|
[$1, name]
|
|
|
|
else
|
|
|
|
[name, "--#{name}"]
|
|
|
|
end
|
|
|
|
end
|
2013-01-23 00:26:23 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
class Options
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
@options = Set.new(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(*args, &block)
|
|
|
|
@options.each(*args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def <<(o)
|
|
|
|
@options << o
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def +(o)
|
|
|
|
Options.new(@options + o)
|
|
|
|
end
|
|
|
|
|
|
|
|
def -(o)
|
|
|
|
Options.new(@options - o)
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:28 -06:00
|
|
|
def &(o)
|
|
|
|
Options.new(@options & o)
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:23 -06:00
|
|
|
def *(arg)
|
|
|
|
@options.to_a * arg
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
@options.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_flags
|
|
|
|
map(&:flag)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include?(o)
|
|
|
|
any? { |opt| opt == o || opt.name == o || opt.flag == o }
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:28 -06:00
|
|
|
def concat(o)
|
|
|
|
o.each { |opt| @options << opt }
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:23 -06:00
|
|
|
def to_a
|
|
|
|
@options.to_a
|
|
|
|
end
|
|
|
|
alias_method :to_ary, :to_a
|
2013-01-23 00:26:28 -06:00
|
|
|
|
2013-05-24 10:56:02 -05:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class}: #{@options.map(&:inspect).join(", ")}>"
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:28 -06:00
|
|
|
def self.coerce(arg)
|
|
|
|
case arg
|
|
|
|
when self then arg
|
|
|
|
when Option then new << arg
|
2013-01-30 11:04:54 -06:00
|
|
|
when Array
|
2013-05-06 16:52:26 -05:00
|
|
|
opts = new
|
|
|
|
arg.each do |a|
|
|
|
|
case a
|
|
|
|
when /^-[^-]+$/
|
|
|
|
a[1..-1].split(//).each { |o| opts << Option.new(o) }
|
|
|
|
else
|
|
|
|
opts << Option.new(a)
|
2013-01-30 11:04:54 -06:00
|
|
|
end
|
2013-05-06 16:52:26 -05:00
|
|
|
end
|
|
|
|
opts
|
2013-01-30 11:04:54 -06:00
|
|
|
else
|
|
|
|
raise TypeError, "Cannot convert #{arg.inspect} to Options"
|
2013-01-23 00:26:28 -06:00
|
|
|
end
|
|
|
|
end
|
2013-01-23 00:26:23 -06:00
|
|
|
end
|