Class: Thor::Option
- Defined in:
- lib/vendor/thor/lib/thor/parser/option.rb
Overview
:nodoc:
Constant Summary collapse
- VALID_TYPES =
[:boolean, :numeric, :hash, :array, :string]
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
Attributes inherited from Argument
#banner, #default, #description, #name, #required, #type
Class Method Summary collapse
-
.parse(key, value) ⇒ Object
This parse quick options given as method_options.
Instance Method Summary collapse
- #human_name ⇒ Object
-
#initialize(name, description = nil, required = nil, type = nil, default = nil, banner = nil, group = nil, aliases = nil) ⇒ Option
constructor
A new instance of Option.
-
#method_missing(method, *args, &block) ⇒ Object
Allow some type predicates as: boolean?, string? and etc.
- #switch_name ⇒ Object
- #usage(padding = 0) ⇒ Object
Methods inherited from Argument
Constructor Details
#initialize(name, description = nil, required = nil, type = nil, default = nil, banner = nil, group = nil, aliases = nil) ⇒ Option
Returns a new instance of Option.
7 8 9 10 11 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 7 def initialize(name, description=nil, required=nil, type=nil, default=nil, =nil, group=nil, aliases=nil) super(name, description, required, type, default, ) @aliases = [*aliases].compact @group = group.to_s.capitalize if group end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Allow some type predicates as: boolean?, string? and etc.
96 97 98 99 100 101 102 103 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 96 def method_missing(method, *args, &block) given = method.to_s.sub(/\?$/, '').to_sym if valid_type?(given) self.type == given else super end end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
3 4 5 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 3 def aliases @aliases end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
3 4 5 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 3 def group @group end |
Class Method Details
.parse(key, value) ⇒ Object
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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 40 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, aliases) end |
Instance Method Details
#human_name ⇒ Object
74 75 76 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 74 def human_name @human_name ||= dasherized? ? undasherize(name) : name end |
#switch_name ⇒ Object
70 71 72 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 70 def switch_name @switch_name ||= dasherized? ? name : dasherize(name) end |
#usage(padding = 0) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/vendor/thor/lib/thor/parser/option.rb', line 78 def usage(padding=0) sample = if && !.to_s.empty? "#{switch_name}=#{}" else switch_name end sample = "[#{sample}]" unless required? if aliases.empty? (" " * padding) << sample else "#{aliases.join(', ')}, #{sample}" end end |