1
1
require 'set'
2
2
require 'thor/base'
3
3
4
- class Thor
4
+ class Thor # rubocop:disable ClassLength
5
5
class << self
6
6
# Allows for custom "Command" package naming.
7
7
#
8
8
# === Parameters
9
9
# name<String>
10
10
# options<Hash>
11
11
#
12
- def package_name ( name , options = { } )
12
+ def package_name ( name , options = { } )
13
13
@package_name = name . nil? || name == '' ? nil : name
14
14
end
15
15
@@ -18,17 +18,17 @@ def package_name(name, options={})
18
18
# ==== Parameters
19
19
# meth<Symbol>:: name of the default command
20
20
#
21
- def default_command ( meth = nil )
21
+ def default_command ( meth = nil )
22
22
@default_command = case meth
23
- when :none
24
- 'help'
25
- when nil
26
- @default_command || from_superclass ( :default_command , 'help' )
27
- else
28
- meth . to_s
29
- end
23
+ when :none
24
+ 'help'
25
+ when nil
26
+ @default_command || from_superclass ( :default_command , 'help' )
27
+ else
28
+ meth . to_s
29
+ end
30
30
end
31
- alias default_task default_command
31
+ alias_method : default_task, : default_command
32
32
33
33
# Registers another Thor subclass as a command.
34
34
#
@@ -37,7 +37,7 @@ def default_command(meth=nil)
37
37
# command<String>:: Subcommand name to use
38
38
# usage<String>:: Short usage for the subcommand
39
39
# description<String>:: Description for the subcommand
40
- def register ( klass , subcommand_name , usage , description , options = { } )
40
+ def register ( klass , subcommand_name , usage , description , options = { } )
41
41
if klass <= Thor ::Group
42
42
desc usage , description , options
43
43
define_method ( subcommand_name ) { |*args | invoke ( klass , args ) }
@@ -54,7 +54,7 @@ def register(klass, subcommand_name, usage, description, options={})
54
54
# description<String>
55
55
# options<String>
56
56
#
57
- def desc ( usage , description , options = { } )
57
+ def desc ( usage , description , options = { } )
58
58
if options [ :for ]
59
59
command = find_and_refresh_command ( options [ :for ] )
60
60
command . usage = usage if usage
@@ -69,7 +69,7 @@ def desc(usage, description, options={})
69
69
# ==== Parameters
70
70
# long description<String>
71
71
#
72
- def long_desc ( long_description , options = { } )
72
+ def long_desc ( long_description , options = { } )
73
73
if options [ :for ]
74
74
command = find_and_refresh_command ( options [ :for ] )
75
75
command . long_description = long_description if long_description
@@ -91,13 +91,13 @@ def long_desc(long_description, options={})
91
91
# ==== Parameters
92
92
# Hash[String|Array => Symbol]:: Maps the string or the strings in the array to the given command.
93
93
#
94
- def map ( mappings = nil )
94
+ def map ( mappings = nil )
95
95
@map ||= from_superclass ( :map , { } )
96
96
97
97
if mappings
98
98
mappings . each do |key , value |
99
99
if key . respond_to? ( :each )
100
- key . each { |subkey | @map [ subkey ] = value }
100
+ key . each { |subkey | @map [ subkey ] = value }
101
101
else
102
102
@map [ key ] = value
103
103
end
@@ -114,13 +114,13 @@ def map(mappings=nil)
114
114
# is the type of the option. Can be :string, :array, :hash, :boolean, :numeric
115
115
# or :required (string). If you give a value, the type of the value is used.
116
116
#
117
- def method_options ( options = nil )
117
+ def method_options ( options = nil )
118
118
@method_options ||= { }
119
119
build_options ( options , @method_options ) if options
120
120
@method_options
121
121
end
122
122
123
- alias options method_options
123
+ alias_method : options, : method_options
124
124
125
125
# Adds an option to the set of method options. If :for is given as option,
126
126
# it allows you to change the options from a previous defined command.
@@ -148,16 +148,16 @@ def method_options(options=nil)
148
148
# :banner - String to show on usage notes.
149
149
# :hide - If you want to hide this option from the help.
150
150
#
151
- def method_option ( name , options = { } )
151
+ def method_option ( name , options = { } )
152
152
scope = if options [ :for ]
153
- find_and_refresh_command ( options [ :for ] ) . options
154
- else
155
- method_options
156
- end
153
+ find_and_refresh_command ( options [ :for ] ) . options
154
+ else
155
+ method_options
156
+ end
157
157
158
158
build_option ( name , options , scope )
159
159
end
160
- alias option method_option
160
+ alias_method : option, : method_option
161
161
162
162
# Prints help information for the given command.
163
163
#
@@ -170,18 +170,18 @@ def command_help(shell, command_name)
170
170
command = all_commands [ meth ]
171
171
handle_no_command_error ( meth ) unless command
172
172
173
- shell . say " Usage:"
173
+ shell . say ' Usage:'
174
174
shell . say " #{ banner ( command ) } "
175
175
shell . say
176
176
class_options_help ( shell , nil => command . options . map { |_ , o | o } )
177
177
if command . long_description
178
- shell . say " Description:"
178
+ shell . say ' Description:'
179
179
shell . print_wrapped ( command . long_description , :indent => 2 )
180
180
else
181
181
shell . say command . description
182
182
end
183
183
end
184
- alias task_help command_help
184
+ alias_method : task_help, : command_help
185
185
186
186
# Prints help information for this class.
187
187
#
@@ -193,12 +193,12 @@ def help(shell, subcommand = false)
193
193
Thor ::Util . thor_classes_in ( self ) . each do |klass |
194
194
list += klass . printable_commands ( false )
195
195
end
196
- list . sort! { |a , b | a [ 0 ] <=> b [ 0 ] }
196
+ list . sort! { |a , b | a [ 0 ] <=> b [ 0 ] }
197
197
198
198
if @package_name
199
199
shell . say "#{ @package_name } commands:"
200
200
else
201
- shell . say " Commands:"
201
+ shell . say ' Commands:'
202
202
end
203
203
204
204
shell . print_table ( list , :indent => 2 , :truncate => true )
@@ -212,34 +212,34 @@ def printable_commands(all = true, subcommand = false)
212
212
next if command . hidden?
213
213
item = [ ]
214
214
item << banner ( command , false , subcommand )
215
- item << ( command . description ? "# #{ command . description . gsub ( /\s +/m , ' ' ) } " : "" )
215
+ item << ( command . description ? "# #{ command . description . gsub ( /\s +/m , ' ' ) } " : '' )
216
216
item
217
217
end . compact
218
218
end
219
- alias printable_tasks printable_commands
219
+ alias_method : printable_tasks, : printable_commands
220
220
221
221
def subcommands
222
222
@subcommands ||= from_superclass ( :subcommands , [ ] )
223
223
end
224
- alias subtasks subcommands
224
+ alias_method : subtasks, : subcommands
225
225
226
226
def subcommand ( subcommand , subcommand_class )
227
- self . subcommands << subcommand . to_s
227
+ subcommands << subcommand . to_s
228
228
subcommand_class . subcommand_help subcommand
229
229
230
230
define_method ( subcommand ) do |*args |
231
231
args , opts = Thor ::Arguments . split ( args )
232
232
invoke subcommand_class , args , opts , :invoked_via_subcommand => true , :class_options => options
233
233
end
234
234
end
235
- alias subtask subcommand
235
+ alias_method : subtask, : subcommand
236
236
237
237
# Extend check unknown options to accept a hash of conditions.
238
238
#
239
239
# === Parameters
240
240
# options<Hash>: A hash containing :only and/or :except keys
241
- def check_unknown_options! ( options = { } )
242
- @check_unknown_options ||= Hash . new
241
+ def check_unknown_options! ( options = { } )
242
+ @check_unknown_options ||= { }
243
243
options . each do |key , value |
244
244
if value
245
245
@check_unknown_options [ key ] = Array ( value )
@@ -320,7 +320,7 @@ def stop_on_unknown_option?(command) #:nodoc:
320
320
protected
321
321
322
322
# The method responsible for dispatching given the args.
323
- def dispatch ( meth , given_args , given_opts , config ) #:nodoc:
323
+ def dispatch ( meth , given_args , given_opts , config ) #:nodoc: # rubocop:disable MethodLength
324
324
# There is an edge case when dispatching from a subcommand.
325
325
# A problem occurs invoking the default command. This case occurs
326
326
# when arguments are passed and a default command is defined, and
@@ -331,7 +331,7 @@ def dispatch(meth, given_args, given_opts, config) #:nodoc:
331
331
# the command normally. If the first item in given_args is not
332
332
# a command then use the default command. The given_args will be
333
333
# intact later since dup was used.
334
- if config [ :invoked_via_subcommand ] && given_args . size >= 1 && default_command != " help" && given_args . first != default_command
334
+ if config [ :invoked_via_subcommand ] && given_args . size >= 1 && default_command != ' help' && given_args . first != default_command
335
335
meth ||= retrieve_command_name ( given_args . dup )
336
336
command = all_commands [ normalize_command_name ( meth ) ]
337
337
command ||= all_commands [ normalize_command_name ( default_command ) ]
@@ -382,16 +382,16 @@ def create_command(meth) #:nodoc:
382
382
commands [ meth ] = base_class . new ( meth , @desc , @long_desc , @usage , method_options )
383
383
@usage , @desc , @long_desc , @method_options , @hide = nil
384
384
true
385
- elsif self . all_commands [ meth ] || meth == " method_missing"
385
+ elsif all_commands [ meth ] || meth == ' method_missing'
386
386
true
387
387
else
388
388
puts "[WARNING] Attempted to create command #{ meth . inspect } without usage or description. " <<
389
- " Call desc if you want this method to be available as command or declare it inside a " <<
389
+ ' Call desc if you want this method to be available as command or declare it inside a ' <<
390
390
"no_commands{} block. Invoked from #{ caller [ 1 ] . inspect } ."
391
391
false
392
392
end
393
393
end
394
- alias create_task create_command
394
+ alias_method : create_task, : create_command
395
395
396
396
def initialize_added #:nodoc:
397
397
class_options . merge! ( method_options )
@@ -407,7 +407,7 @@ def retrieve_command_name(args) #:nodoc:
407
407
nil
408
408
end
409
409
end
410
- alias retrieve_task_name retrieve_command_name
410
+ alias_method : retrieve_task_name, : retrieve_command_name
411
411
412
412
# receives a (possibly nil) command name and returns a name that is in
413
413
# the commands hash. In addition to normalizing aliases, this logic
@@ -421,7 +421,7 @@ def normalize_command_name(meth) #:nodoc:
421
421
422
422
possibilities = find_command_possibilities ( meth )
423
423
if possibilities . size > 1
424
- raise AmbiguousTaskError , "Ambiguous command #{ meth } matches [#{ possibilities . join ( ', ' ) } ]"
424
+ fail AmbiguousTaskError , "Ambiguous command #{ meth } matches [#{ possibilities . join ( ', ' ) } ]"
425
425
elsif possibilities . size < 1
426
426
meth = meth || default_command
427
427
elsif map [ meth ]
@@ -430,9 +430,9 @@ def normalize_command_name(meth) #:nodoc:
430
430
meth = possibilities . first
431
431
end
432
432
433
- meth . to_s . gsub ( '-' , '_' ) # treat foo-bar as foo_bar
433
+ meth . to_s . gsub ( '-' , '_' ) # treat foo-bar as foo_bar
434
434
end
435
- alias normalize_task_name normalize_command_name
435
+ alias_method : normalize_task_name, : normalize_command_name
436
436
437
437
# this is the logic that takes the command name passed in by the user
438
438
# and determines whether it is an unambiguous substrings of a command or
@@ -450,23 +450,22 @@ def find_command_possibilities(meth)
450
450
possibilities
451
451
end
452
452
end
453
- alias find_task_possibilities find_command_possibilities
453
+ alias_method : find_task_possibilities, : find_command_possibilities
454
454
455
455
def subcommand_help ( cmd )
456
- desc " help [COMMAND]" , " Describe subcommands or one specific subcommand"
457
- class_eval <<-RUBY
456
+ desc ' help [COMMAND]' , ' Describe subcommands or one specific subcommand'
457
+ class_eval "
458
458
def help(command = nil, subcommand = true); super; end
459
- RUBY
459
+ "
460
460
end
461
- alias subtask_help subcommand_help
462
-
461
+ alias_method :subtask_help , :subcommand_help
463
462
end
464
463
465
464
include Thor ::Base
466
465
467
466
map HELP_MAPPINGS => :help
468
467
469
- desc " help [COMMAND]" , " Describe available commands or one specific command"
468
+ desc ' help [COMMAND]' , ' Describe available commands or one specific command'
470
469
def help ( command = nil , subcommand = false )
471
470
command ? self . class . command_help ( shell , command ) : self . class . help ( shell , subcommand )
472
471
end
0 commit comments