Class: Thor::Option
- Inherits:
-
Argument
show all
- Includes:
- NRSER::Log::Mixin
- Defined in:
- lib/thor/parser/option.rb
Overview
Constant Summary
collapse
- VALID_TYPES =
[:boolean, :numeric, :hash, :array, :string]
Instance Attribute Summary collapse
Attributes inherited from Argument
#banner, #complete, #default, #description, #enum, #name, #required, #type
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Argument
#default_banner, #required?, #show_default?, #valid_type?
Constructor Details
#initialize(name, options = {}) ⇒ Option
Returns a new instance of Option.
12
13
14
15
16
17
18
19
20
|
# File 'lib/thor/parser/option.rb', line 12
def initialize(name, options = {})
@check_default_type = options[:check_default_type]
options[:required] = false unless options.key?(:required)
super
@lazy_default = options[:lazy_default]
@group = options[:group].to_s.titleize if options[:group]
@aliases = Array(options[:aliases])
@hide = options[:hide]
end
|
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
8
9
10
|
# File 'lib/thor/parser/option.rb', line 8
def aliases
@aliases
end
|
#group ⇒ Object
Returns the value of attribute group.
8
9
10
|
# File 'lib/thor/parser/option.rb', line 8
def group
@group
end
|
#hide ⇒ Object
Returns the value of attribute hide.
8
9
10
|
# File 'lib/thor/parser/option.rb', line 8
def hide
@hide
end
|
#lazy_default ⇒ Object
Returns the value of attribute lazy_default.
8
9
10
|
# File 'lib/thor/parser/option.rb', line 8
def lazy_default
@lazy_default
end
|
Class Method Details
.alias_to_switch_name(alias_name) ⇒ Object
84
85
86
|
# File 'lib/thor/parser/option.rb', line 84
def self.alias_to_switch_name alias_name
alias_name.sub( /\A\-{1,2}/, '' ).dasherize
end
|
.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"
parse [:foo, :baz] => "bar"
parse :foo => :required
parse :foo => 2
parse :foo => :numeric
parse :foo => true
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.
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
76
77
|
# File 'lib/thor/parser/option.rb', line 49
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) :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
#all_switch_names ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'lib/thor/parser/option.rb', line 89
def all_switch_names
[
name.dasherize,
*aliases.map { |alias_name|
self.class.alias_to_switch_name( alias_name )
}
]
end
|
#all_switch_tokens ⇒ Object
125
126
127
|
# File 'lib/thor/parser/option.rb', line 125
def all_switch_tokens
[*long_switch_tokens, *short_switch_tokens]
end
|
#dasherize(str) ⇒ Object
194
195
196
|
# File 'lib/thor/parser/option.rb', line 194
def dasherize(str)
(str.length > 1 ? "--" : "-") + str.tr("_", "-")
end
|
#dasherized? ⇒ Boolean
186
187
188
|
# File 'lib/thor/parser/option.rb', line 186
def dasherized?
name.index("-") == 0
end
|
#human_name ⇒ Object
130
131
132
|
# File 'lib/thor/parser/option.rb', line 130
def human_name
@human_name ||= dasherized? ? undasherize(name) : name
end
|
#long_switch_names ⇒ Object
109
110
111
|
# File 'lib/thor/parser/option.rb', line 109
def long_switch_names
all_switch_names.select { |name| name.length > 1 }
end
|
#long_switch_tokens ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'lib/thor/parser/option.rb', line 114
def long_switch_tokens
if boolean?
long_switch_names.flat_map { |name|
[ "--#{ name }", "--no-#{ name }" ]
}
else
long_switch_names.map { |name| "--#{ name }" }
end
end
|
#short_switch_names ⇒ Object
99
100
101
|
# File 'lib/thor/parser/option.rb', line 99
def short_switch_names
all_switch_names.select { |name| name.length == 1 }
end
|
#short_switch_tokens ⇒ Object
104
105
106
|
# File 'lib/thor/parser/option.rb', line 104
def short_switch_tokens
short_switch_names.map { |name| "-#{ name }" }
end
|
#switch_name ⇒ Object
79
80
81
|
# File 'lib/thor/parser/option.rb', line 79
def switch_name
@switch_name ||= dasherized? ? name : dasherize(name)
end
|
#undasherize(str) ⇒ Object
190
191
192
|
# File 'lib/thor/parser/option.rb', line 190
def undasherize(str)
str.sub(/^-{1,2}/, "")
end
|
#usage(padding = 0) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/thor/parser/option.rb', line 134
def usage(padding = 0)
sample = if banner && !banner.to_s.empty?
"#{switch_name}=#{banner}".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
164
165
166
167
|
# File 'lib/thor/parser/option.rb', line 164
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/thor/parser/option.rb', line 169
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
|