Class: Thor::Option
Overview
:nodoc:
Direct Known Subclasses
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.
-
#hide ⇒ Object
readonly
Returns the value of attribute hide.
-
#lazy_default ⇒ Object
readonly
Returns the value of attribute lazy_default.
Attributes inherited from Argument
#banner, #default, #description, #enum, #name, #required, #type
Class Method Summary collapse
-
.parse(key, value) ⇒ Object
This parse quick options given as method_options.
Instance Method Summary collapse
- #dasherize(str) ⇒ Object protected
- #dasherized? ⇒ Boolean protected
- #human_name ⇒ Object
-
#initialize(name, options = {}) ⇒ Option
constructor
A new instance of Option.
- #switch_name ⇒ Object
- #undasherize(str) ⇒ Object protected
- #usage(padding = 0) ⇒ Object
- #validate! ⇒ Object protected
- #validate_default_type! ⇒ Object protected
Methods inherited from Argument
#default_banner, #required?, #show_default?, #valid_type?
Constructor Details
#initialize(name, options = {}) ⇒ Option
Returns a new instance of Option.
10 11 12 13 14 15 16 17 18 |
# File 'lib/thor/parser/option.rb', line 10 def initialize(name, = {}) @check_default_type = [:check_default_type] [:required] = false unless .key?(:required) super @lazy_default = [:lazy_default] @group = [:group].to_s.titleize if [:group] @aliases = Array([:aliases]) @hide = [:hide] end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
6 7 8 |
# File 'lib/thor/parser/option.rb', line 6 def aliases @aliases end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
6 7 8 |
# File 'lib/thor/parser/option.rb', line 6 def group @group end |
#hide ⇒ Object (readonly)
Returns the value of attribute hide.
6 7 8 |
# File 'lib/thor/parser/option.rb', line 6 def hide @hide end |
#lazy_default ⇒ Object (readonly)
Returns the value of attribute lazy_default.
6 7 8 |
# File 'lib/thor/parser/option.rb', line 6 def lazy_default @lazy_default 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/thor/parser/option.rb', line 47 def self.parse(key, value) if key.is_a?(Array) name, *aliases = key else name = key aliases = [] end name = name.to_s default = value type = case value when Symbol default = nil if VALID_TYPES.include?(value) value elsif required = (value == :required) # rubocop:disable AssignmentInCondition :string end when TrueClass, FalseClass :boolean when Numeric :numeric when Hash, Array, String value.class.name.downcase.to_sym end new(name.to_s, :required => required, :type => type, :default => default, :aliases => aliases) end |
Instance Method Details
#dasherize(str) ⇒ Object (protected)
145 146 147 |
# File 'lib/thor/parser/option.rb', line 145 def dasherize(str) (str.length > 1 ? "--" : "-") + str.tr("_", "-") end |
#dasherized? ⇒ Boolean (protected)
137 138 139 |
# File 'lib/thor/parser/option.rb', line 137 def dasherized? name.index("-") == 0 end |
#human_name ⇒ Object
81 82 83 |
# File 'lib/thor/parser/option.rb', line 81 def human_name @human_name ||= dasherized? ? undasherize(name) : name end |
#switch_name ⇒ Object
77 78 79 |
# File 'lib/thor/parser/option.rb', line 77 def switch_name @switch_name ||= dasherized? ? name : dasherize(name) end |
#undasherize(str) ⇒ Object (protected)
141 142 143 |
# File 'lib/thor/parser/option.rb', line 141 def undasherize(str) str.sub(/^-{1,2}/, "") end |
#usage(padding = 0) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/thor/parser/option.rb', line 85 def usage(padding = 0) sample = if && !.to_s.empty? "#{switch_name}=#{}".dup else switch_name end sample = "[#{sample}]".dup unless required? if boolean? sample << ", [#{dasherize('no-' + human_name)}]" unless (name == "force") || name.start_with?("no-") end if aliases.empty? (" " * padding) << sample else "#{aliases.join(', ')}, #{sample}" end end |
#validate! ⇒ Object (protected)
115 116 117 118 |
# File 'lib/thor/parser/option.rb', line 115 def validate! raise ArgumentError, "An option cannot be boolean and required." if boolean? && required? validate_default_type! if @check_default_type end |
#validate_default_type! ⇒ Object (protected)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/thor/parser/option.rb', line 120 def validate_default_type! default_type = case @default when nil return when TrueClass, FalseClass required? ? :string : :boolean when Numeric :numeric when Symbol :string when Hash, Array, String @default.class.name.downcase.to_sym end raise ArgumentError, "Expected #{@type} default value for '#{switch_name}'; got #{@default.inspect} (#{default_type})" unless default_type == @type end |