Skip to content

Commit

Permalink
Merge pull request #387 from developingchris/unify_help
Browse files Browse the repository at this point in the history
Make subcommand help more consistent
  • Loading branch information
sferik committed Mar 22, 2014
2 parents dc13fc6 + e917677 commit 54a0e17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,14 @@ def subcommands
end
alias_method :subtasks, :subcommands

def subcommand_classes
@subcommand_classes ||= {}
end

def subcommand(subcommand, subcommand_class)
subcommands << subcommand.to_s
subcommand_class.subcommand_help subcommand
subcommand_classes[subcommand.to_s] = subcommand_class

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

desc 'help [COMMAND]', 'Describe available commands or one specific command'
def help(command = nil, subcommand = false)
command ? self.class.command_help(shell, command) : self.class.help(shell, subcommand)
if command
if self.class.subcommands.include? command
self.class.subcommand_classes[command].help(shell, true)
else
self.class.command_help(shell, command)
end
else
self.class.help(shell, subcommand)
end
end
end
5 changes: 5 additions & 0 deletions spec/subcommand_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
expect(output).to eq(sub_help)
end

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

end

0 comments on commit 54a0e17

Please sign in to comment.