Class: Thor::Option
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.
-
#hide ⇒ Object
readonly
Returns the value of attribute hide.
-
#lazy_default ⇒ Object
readonly
Returns the value of attribute lazy_default.
-
#repeatable ⇒ Object
readonly
Returns the value of attribute repeatable.
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
- #aliases_for_usage ⇒ Object
- #human_name ⇒ Object
-
#initialize(name, options = {}) ⇒ Option
constructor
A new instance of Option.
- #show_default? ⇒ Boolean
- #switch_name ⇒ Object
- #usage(padding = 0) ⇒ Object
Methods inherited from Argument
#enum_to_s, #print_default, #required?
Constructor Details
#initialize(name, options = {}) ⇒ Option
Returns a new instance of Option.
7 8 9 10 11 12 13 14 15 16 |
# File '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 |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
3 4 5 |
# File '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/thor/parser/option.rb', line 3 def group @group end |
#hide ⇒ Object (readonly)
Returns the value of attribute hide.
3 4 5 |
# File 'lib/thor/parser/option.rb', line 3 def hide @hide end |
#lazy_default ⇒ Object (readonly)
Returns the value of attribute lazy_default.
3 4 5 |
# File 'lib/thor/parser/option.rb', line 3 def lazy_default @lazy_default end |
#repeatable ⇒ Object (readonly)
Returns the value of attribute repeatable.
3 4 5 |
# File 'lib/thor/parser/option.rb', line 3 def repeatable @repeatable 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.
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 70 71 72 73 |
# File '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 Method Details
#aliases_for_usage ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/thor/parser/option.rb', line 99 def aliases_for_usage if aliases.empty? "" else "#{aliases.join(', ')}, " end end |
#human_name ⇒ Object
79 80 81 |
# File 'lib/thor/parser/option.rb', line 79 def human_name @human_name ||= dasherized? ? undasherize(name) : name end |
#show_default? ⇒ Boolean
107 108 109 110 111 112 113 114 |
# File 'lib/thor/parser/option.rb', line 107 def show_default? case default when TrueClass, FalseClass true else super end end |
#switch_name ⇒ Object
75 76 77 |
# File 'lib/thor/parser/option.rb', line 75 def switch_name @switch_name ||= dasherized? ? name : dasherize(name) end |
#usage(padding = 0) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File '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 |