class Thor::Option
:nodoc:
def self.parse(key, value)
By default all options are optional, unless :required is given.
string (--foo=value) or booleans (just --foo).
is given a default type is assumed. This default type accepts arguments as
The valid types are :boolean, :numeric, :hash, :array and :string. If none
#=> Option foo with default value true and type boolean
parse :foo => true
#=> Option foo without default value and type numeric
parse :foo => :numeric
#=> Option foo with default value 2 and type numeric
parse :foo => 2
#=> Required option foo without default value
parse :foo => :required
#=> Option foo with default value bar and alias :baz
parse [:foo, :baz] => "bar"
#=> Option foo with default value bar
parse :foo => "bar"
assumptions, but you can be more specific using the option method.
This parse quick options given as method_options. It makes several
def self.parse(key, value) if key.is_a?(Array) name, *aliases = key else name, aliases = key, [] end name = name.to_s default = value type = case value when Symbol default = nil if VALID_TYPES.include?(value) value elsif required = (value == :required) :string end when TrueClass, FalseClass :boolean when Numeric :numeric when Hash, Array, String value.class.name.downcase.to_sym end self.new(name.to_s, nil, required, type, default, nil, nil, nil, aliases) end
def dasherize(str)
def dasherize(str) (str.length > 1 ? "--" : "-") + str.gsub('_', '-') end
def dasherized?
def dasherized? name.index('-') == 0 end
def human_name
def human_name @human_name ||= dasherized? ? undasherize(name) : name end
def initialize(name, description=nil, required=nil, type=nil, default=nil, banner=nil, lazy_default=nil, group=nil, aliases=nil)
def initialize(name, description=nil, required=nil, type=nil, default=nil, banner=nil, lazy_default=nil, group=nil, aliases=nil) super(name, description, required, type, default, banner) @lazy_default = lazy_default @group = group.to_s.capitalize if group @aliases = [*aliases].compact end
def switch_name
def switch_name @switch_name ||= dasherized? ? name : dasherize(name) end
def undasherize(str)
def undasherize(str) str.sub(/^-{1,2}/, '') end
def usage(padding=0)
def usage(padding=0) sample = if banner && !banner.to_s.empty? "#{switch_name}=#{banner}" else switch_name end sample = "[#{sample}]" unless required? if aliases.empty? (" " * padding) << sample else "#{aliases.join(', ')}, #{sample}" end end
def validate!
def validate! raise ArgumentError, "An option cannot be boolean and required." if boolean? && required? end