Add '--help' to any command to immediately get help for that command.

This commit is contained in:
Mitchell Hashimoto 2010-04-25 16:58:35 -07:00
parent bbb1b70e1d
commit c28195ed26
2 changed files with 25 additions and 19 deletions

View File

@ -50,22 +50,6 @@ module Vagrant
klass.dispatch(env, *args)
end
# Prints out the list of supported commands and their descriptions (if
# available) then exits.
def puts_help
puts "Usage: vagrant SUBCOMMAND ...\n\n"
puts "Supported commands:"
subcommands.keys.sort.each do |key|
klass = subcommands[key]
next if klass.description.empty?
puts "#{' ' * 4}#{key.ljust(20)}#{klass.description}"
end
exit
end
# Sets or reads the description, depending on if the value is set in the
# parameter.
def description(value=nil)
@ -92,7 +76,7 @@ module Vagrant
else
# Just print out the help, since this top-level command does nothing
# on its own
self.class.puts_help
show_help
end
end
@ -122,6 +106,12 @@ module Vagrant
def option_parser(reload=false)
@option_parser = nil if reload
@option_parser ||= OptionParser.new do |opts|
# The --help flag is available on all children commands, and will
# immediately show help.
opts.on("--help", "Show help for the current subcommand.") do
show_help
end
options_spec(opts)
end
end
@ -156,6 +146,20 @@ module Vagrant
end
puts option_parser.help
my_klass = self.class
if !my_klass.subcommands.empty?
puts "\nSupported subcommands:"
my_klass.subcommands.keys.sort.each do |key|
klass = my_klass.subcommands[key]
next if klass.description.empty?
puts "#{' ' * 8}#{key.ljust(20)}#{klass.description}"
end
puts "\nFor help on a specific subcommand, run `vagrant SUBCOMMAND --help`"
end
exit
end
end

View File

@ -67,13 +67,13 @@ class CommandsBaseTest < Test::Unit::TestCase
context "executing" do
should "show version if flag is set" do
@instance.expects(:puts_version).once
@instance.expects(:puts_help).never
@instance.expects(:show_help).never
@instance.execute(["--version"])
end
should "just print the help by default" do
@instance.expects(:puts_version).never
@klass.expects(:puts_help)
@instance.expects(:show_help).once
@instance.execute([])
end
end
@ -81,6 +81,8 @@ class CommandsBaseTest < Test::Unit::TestCase
context "getting the option parser" do
should "create it with the options spec if it hasn't been created yet" do
opts = mock("opts")
opts.stubs(:on)
result = mock("result")
OptionParser.expects(:new).yields(opts).returns(result)
@instance.expects(:options_spec).with(opts)