2013-01-30 11:04:54 -06:00
|
|
|
require 'set'
|
|
|
|
|
2013-01-23 00:26:23 -06:00
|
|
|
class Option
|
|
|
|
attr_reader :name, :description, :flag
|
|
|
|
|
2014-08-07 00:48:13 -05:00
|
|
|
def initialize(name, description="")
|
2013-01-23 00:26:28 -06:00
|
|
|
@name, @flag = split_name(name)
|
2014-08-07 00:48:13 -05:00
|
|
|
@description = description
|
2013-01-23 00:26:23 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
flag
|
|
|
|
end
|
|
|
|
alias_method :to_str, :to_s
|
|
|
|
|
|
|
|
def <=>(other)
|
2014-06-09 14:56:22 -05:00
|
|
|
return unless Option === other
|
2013-01-23 00:26:23 -06:00
|
|
|
name <=> other.name
|
|
|
|
end
|
|
|
|
|
2014-07-03 19:29:18 -05:00
|
|
|
def ==(other)
|
2013-06-27 01:18:32 -05:00
|
|
|
instance_of?(other.class) && name == other.name
|
2013-01-23 00:26:23 -06:00
|
|
|
end
|
2014-07-03 19:29:18 -05:00
|
|
|
alias_method :eql?, :==
|
2013-01-23 00:26:23 -06:00
|
|
|
|
|
|
|
def hash
|
|
|
|
name.hash
|
|
|
|
end
|
2013-01-23 00:26:28 -06:00
|
|
|
|
2013-05-24 10:56:02 -05:00
|
|
|
def inspect
|
2014-07-01 15:07:06 -05:00
|
|
|
"#<#{self.class.name}: #{flag.inspect}>"
|
2013-05-24 10:56:02 -05:00
|
|
|
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
|
|
|
|
|
2013-08-22 17:45:08 -05:00
|
|
|
attr_reader :options
|
|
|
|
protected :options
|
|
|
|
|
2014-08-13 11:09:57 -05:00
|
|
|
def self.create(array)
|
|
|
|
options = new
|
|
|
|
array.each do |e|
|
|
|
|
case e
|
|
|
|
when /^-[^-]+$/
|
|
|
|
e[1..-1].split(//).each { |o| options << Option.new(o) }
|
|
|
|
else
|
|
|
|
options << Option.new(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:23 -06:00
|
|
|
def initialize(*args)
|
|
|
|
@options = Set.new(*args)
|
|
|
|
end
|
|
|
|
|
2013-08-22 11:46:47 -05:00
|
|
|
def initialize_copy(other)
|
|
|
|
super
|
2013-08-22 17:45:08 -05:00
|
|
|
@options = other.options.dup
|
2013-08-22 11:46:47 -05:00
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:23 -06:00
|
|
|
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
|
|
|
|
|
2014-02-27 14:22:42 -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
|
|
|
|
|
|
|
|
alias_method :to_ary, :to_a
|
2013-01-23 00:26:28 -06:00
|
|
|
|
2013-05-24 10:56:02 -05:00
|
|
|
def inspect
|
2014-07-01 15:07:06 -05:00
|
|
|
"#<#{self.class.name}: #{to_a.inspect}>"
|
2013-05-24 10:56:02 -05:00
|
|
|
end
|
2013-01-23 00:26:23 -06:00
|
|
|
end
|