Class: Bundler::Thor::Option
Do not use. This class is for internal use only.
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Argument
|
|
Instance Chain:
self,
Argument
|
|
Inherits: |
Bundler::Thor::Argument
|
Defined in: | lib/bundler/vendor/thor/lib/thor/parser/option.rb |
Constant Summary
-
VALID_TYPES =
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 5[:boolean, :numeric, :hash, :array, :string]
Argument
- Inherited
Class Method Summary
- .new(name, options = {}) ⇒ Option constructor
-
.parse(key, value)
This parse quick options given as method_options.
Argument
- Inherited
Instance Attribute Summary
- #aliases readonly
- #group readonly
- #hide readonly
- #lazy_default readonly
- #repeatable readonly
- #show_default? ⇒ Boolean readonly
- #dasherized? ⇒ Boolean readonly protected
Argument
- Inherited
#banner, #default, #description, #enum, | |
#human_name | Alias for Argument#name. |
#name, #required, #required?, #show_default?, #type |
Instance Method Summary
- #aliases_for_usage
- #human_name
- #switch_name
- #usage(padding = 0)
- #dasherize(str) protected
- #undasherize(str) protected
- #validate! protected
- #validate_default_type! protected
- #normalize_aliases(aliases) private
Argument
- Inherited
Constructor Details
.new(name, options = {}) ⇒ Option
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 7
def initialize(name, = {}) @check_default_type = [:check_default_type] [:required] = false unless .key?(:required) @repeatable = .fetch(:repeatable, false) super @lazy_default = [:lazy_default] @group = [:group].to_s.capitalize if [:group] @aliases = normalize_aliases( [:aliases]) @hide = [:hide] end
Class Method Details
.parse(key, value)
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.
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 45
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 Lint/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 Attribute Details
#aliases (readonly)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3
attr_reader :aliases, :group, :lazy_default, :hide, :repeatable
#dasherized? ⇒ Boolean
(readonly, protected)
[ GitHub ]
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 160
def dasherized? name.index("-") == 0 end
#group (readonly)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3
attr_reader :aliases, :group, :lazy_default, :hide, :repeatable
#hide (readonly)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3
attr_reader :aliases, :group, :lazy_default, :hide, :repeatable
#lazy_default (readonly)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3
attr_reader :aliases, :group, :lazy_default, :hide, :repeatable
#repeatable (readonly)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3
attr_reader :aliases, :group, :lazy_default, :hide, :repeatable
#show_default? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 107
def show_default? case default when TrueClass, FalseClass true else super end end
Instance Method Details
#aliases_for_usage
[ GitHub ]#dasherize(str) (protected)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 168
def dasherize(str) (str.length > 1 ? "--" : "-") + str.tr("_", "-") end
#human_name
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 79
def human_name @human_name ||= dasherized? ? undasherize(name) : name end
#normalize_aliases(aliases) (private)
[ GitHub ]#switch_name
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 75
def switch_name @switch_name ||= dasherized? ? name : dasherize(name) end
#undasherize(str) (protected)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 164
def undasherize(str) str.sub(/^-{1,2}/, "") end
#usage(padding = 0)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 83
def usage(padding = 0) sample = if && ! .to_s.empty? "#{switch_name}=#{}".dup else switch_name end sample = "[#{sample}]".dup unless required? if boolean? && name != "force" && !name.match(/\A(no|skip)[\-_]/) sample << ", [#{dasherize('no-' + human_name)}], [#{dasherize('skip-' + human_name)}]" end aliases_for_usage.ljust(padding) + sample end
#validate! (protected)
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 126
def validate! raise ArgumentError, "An option cannot be boolean and required." if boolean? && required? validate_default_type! end
#validate_default_type! (protected)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 131
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 expected_type = (@repeatable && @type != :hash) ? :array : @type if default_type != expected_type err = "Expected #{expected_type} default value for '#{switch_name}'; got #{@default.inspect} (#{default_type})" if @check_default_type raise ArgumentError, err elsif @check_default_type == nil Bundler::Thor.deprecation_warning "#{err}.\n" + "This will be rejected in the future unless you explicitly pass the options `check_default_type: false`" + " or call `allow_incompatible_default_type!` in your code" end end end