classThorclassOption<Argument#:nodoc:attr_reader:aliases,:groupVALID_TYPES=[:boolean,:numeric,:hash,:array,:string]definitialize(name,description=nil,required=nil,type=nil,default=nil,banner=nil,group=nil,aliases=nil)super(name,description,required,type,default,banner)@aliases=[*aliases].compact@group=group.to_s.capitalizeifgroupend# This parse quick options given as method_options. It makes several# assumptions, but you can be more specific using the option method.## parse :foo => "bar"# #=> Option foo with default value bar## parse [:foo, :baz] => "bar"# #=> Option foo with default value bar and alias :baz## parse :foo => :required# #=> Required option foo without default value## parse :foo => 2# #=> Option foo with default value 2 and type numeric## parse :foo => :numeric# #=> Option foo without default value and type numeric## parse :foo => true# #=> Option foo with default value true and type boolean## The valid types are :boolean, :numeric, :hash, :array and :string. If none# is given a default type is assumed. This default type accepts arguments as# string (--foo=value) or booleans (just --foo).## By default all options are optional, unless :required is given.# defself.parse(key,value)ifkey.is_a?(Array)name,*aliases=keyelsename,aliases=key,[]endname=name.to_sdefault=valuetype=casevaluewhenSymboldefault=nilifVALID_TYPES.include?(value)valueelsifrequired=(value==:required):stringelsifvalue==:optional# TODO Remove this warning in the future.warn"Optional type is deprecated. Choose :boolean or :string instead. Assumed to be :boolean.":booleanendwhenTrueClass,FalseClass:booleanwhenNumeric:numericwhenHash,Array,Stringvalue.class.name.downcase.to_symendself.new(name.to_s,nil,required,type,default,nil,nil,aliases)enddefswitch_name@switch_name||=dasherized??name:dasherize(name)enddefhuman_name@human_name||=dasherized??undasherize(name):nameenddefusage(padding=0)sample=ifbanner&&!banner.to_s.empty?"#{switch_name}=#{banner}"elseswitch_nameendsample="[#{sample}]"unlessrequired?ifaliases.empty?(" "*padding)<<sampleelse"#{aliases.join(', ')}, #{sample}"endend# Allow some type predicates as: boolean?, string? and etc.#defmethod_missing(method,*args,&block)given=method.to_s.sub(/\?$/,'').to_symifvalid_type?(given)self.type==givenelsesuperendendprotecteddefvalidate!raiseArgumentError,"An option cannot be boolean and required."ifboolean?&&required?enddefvalid_type?(type)VALID_TYPES.include?(type.to_sym)enddefdasherized?name.index('-')==0enddefundasherize(str)str.sub(/^-{1,2}/,'')enddefdasherize(str)(str.length>1?"--":"-")+str.gsub('_','-')endendend