Class: Bundler::Thor::Option

Inherits:
Argument show all
Defined in:
lib/bundler/vendor/thor/lib/thor/parser/option.rb

Overview

:nodoc:

Constant Summary collapse

VALID_TYPES =
[:boolean, :numeric, :hash, :array, :string]

Instance Attribute Summary collapse

Attributes inherited from Argument

#banner, #default, #description, #enum, #name, #required, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Argument

#required?, #show_default?

Constructor Details

#initialize(name, options = {}) ⇒ Option

Returns a new instance of Option.



7
8
9
10
11
12
13
14
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 7

def initialize(name, options = {})
  options[:required] = false unless options.key?(:required)
  super
  @lazy_default = options[:lazy_default]
  @group        = options[:group].to_s.capitalize if options[:group]
  @aliases      = Array(options[:aliases])
  @hide         = options[:hide]
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



3
4
5
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3

def aliases
  @aliases
end

#groupObject (readonly)

Returns the value of attribute group.



3
4
5
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3

def group
  @group
end

#hideObject (readonly)

Returns the value of attribute hide.



3
4
5
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3

def hide
  @hide
end

#lazy_defaultObject (readonly)

Returns the value of attribute lazy_default.



3
4
5
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 3

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.



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
69
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 43

def self.parse(key, value) # rubocop:disable MethodLength
  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) # 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

#human_nameObject



75
76
77
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 75

def human_name
  @human_name ||= dasherized? ? undasherize(name) : name
end

#switch_nameObject



71
72
73
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 71

def switch_name
  @switch_name ||= dasherized? ? name : dasherize(name)
end

#usage(padding = 0) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bundler/vendor/thor/lib/thor/parser/option.rb', line 79

def usage(padding = 0)
  sample = if banner && !banner.to_s.empty?
    "#{switch_name}=#{banner}"
  else
    switch_name
  end

  sample = "[#{sample}]" unless required?

  if boolean?
    sample << ", [#{dasherize("no-" + human_name)}]" unless name == "force"
  end

  if aliases.empty?
    (" " * padding) << sample
  else
    "#{aliases.join(', ')}, #{sample}"
  end
end