Skip to content

Commit 54a0e17

Browse files
committed
Merge pull request #387 from developingchris/unify_help
Make subcommand help more consistent
2 parents dc13fc6 + e917677 commit 54a0e17

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
@@ -220,9 +220,14 @@ def subcommands
220220
end
221221
alias_method :subtasks, :subcommands
222222

223+
def subcommand_classes
224+
@subcommand_classes ||= {}
225+
end
226+
223227
def subcommand(subcommand, subcommand_class)
224228
subcommands << subcommand.to_s
225229
subcommand_class.subcommand_help subcommand
230+
subcommand_classes[subcommand.to_s] = subcommand_class
226231

227232
define_method(subcommand) do |*args|
228233
args, opts = Thor::Arguments.split(args)
@@ -466,6 +471,14 @@ def help(command = nil, subcommand = true); super; end
466471

467472
desc 'help [COMMAND]', 'Describe available commands or one specific command'
468473
def help(command = nil, subcommand = false)
469-
command ? self.class.command_help(shell, command) : self.class.help(shell, subcommand)
474+
if command
475+
if self.class.subcommands.include? command
476+
self.class.subcommand_classes[command].help(shell, true)
477+
else
478+
self.class.command_help(shell, command)
479+
end
480+
else
481+
self.class.help(shell, subcommand)
482+
end
470483
end
471484
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)