Skip to content

Commit e917677

Browse files
Make subcommand help more consistent
When a command is being asked for help upon and is in the subcommands list the subcommand's class is invoked for help instead. A hash of the subcommand classes was added to facilitate this.
1 parent 3dd0e8f commit e917677

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/thor.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,14 @@ def subcommands
223223
end
224224
alias_method :subtasks, :subcommands
225225

226+
def subcommand_classes
227+
@subcommand_classes ||= {}
228+
end
229+
226230
def subcommand(subcommand, subcommand_class)
227231
subcommands << subcommand.to_s
228232
subcommand_class.subcommand_help subcommand
233+
subcommand_classes[subcommand.to_s] = subcommand_class
229234

230235
define_method(subcommand) do |*args|
231236
args, opts = Thor::Arguments.split(args)
@@ -472,6 +477,14 @@ def help(command = nil, subcommand = true); super; end
472477

473478
desc 'help [COMMAND]', 'Describe available commands or one specific command'
474479
def help(command = nil, subcommand = false)
475-
command ? self.class.command_help(shell, command) : self.class.help(shell, subcommand)
480+
if command
481+
if self.class.subcommands.include? command
482+
self.class.subcommand_classes[command].help(shell, true)
483+
else
484+
self.class.command_help(shell, command)
485+
end
486+
else
487+
self.class.help(shell, subcommand)
488+
end
476489
end
477490
end

spec/subcommand_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
expect(output).to eq(sub_help)
3939
end
4040

41+
it "the help command on the subcommand and after it should result in the same output" do
42+
output = capture(:stdout) { TestSubcommands::Parent.start(%w[sub help])}
43+
sub_help = capture(:stdout) { TestSubcommands::Parent.start(%w[help sub])}
44+
expect(output).to eq(sub_help)
45+
end
4146
end
4247

4348
end

0 commit comments

Comments
 (0)