`vagrant --version` outputs the version

This commit is contained in:
Mitchell Hashimoto 2010-04-13 15:45:38 -07:00
parent b4d1ee6e83
commit 95592eb7f6
3 changed files with 38 additions and 7 deletions

View File

@ -20,8 +20,8 @@ module Vagrant
# Execute a subcommand with the given name and args. This method properly
# finds the subcommand, instantiates it, and executes.
def subcommand(name, *args)
Commands::Base.dispatch(env, name, *args)
def subcommand(*args)
Commands::Base.dispatch(env, *args)
end
end
end

View File

@ -3,7 +3,10 @@ require 'optparse'
module Vagrant
class Commands
# This is the base command class which all sub-commands must
# inherit from.
# inherit from. Subclasses of bases are expected to implement two
# methods: {#execute} and {#options_spec} (optional). The former
# defines the actual behavior of the command while the latter is a spec
# outlining the options that the command may take.
class Base
include Util
@ -79,15 +82,36 @@ module Vagrant
# executed. The `args` parameter is an array of parameters to the
# command (similar to ARGV)
def execute(args)
parse_options(args)
if options[:version]
puts_version
else
# Just print out the help, since this top-level command does nothing
# on its own
self.class.puts_help
end
end
# This method is called by the base class to get the `optparse` configuration
# for the command.
def options_spec(opts)
opts.banner = "Usage: vagrant SUBCOMMAND"
opts.on("--version", "Output running Vagrant version.") do |v|
options[:version] = v
end
end
#-------------------------------------------------------------------
# Methods below are not meant to be overriden/implemented by subclasses
#-------------------------------------------------------------------
# Shows the version
def puts_version
File.open(File.join(PROJECT_ROOT, "VERSION"), "r") do |f|
puts f.read
end
end
# Returns the `OptionParser` instance to be used with this subcommand,

View File

@ -65,7 +65,14 @@ class CommandsBaseTest < Test::Unit::TestCase
end
context "executing" do
should "just print the help" do
should "show version if flag is set" do
@instance.expects(:puts_version).once
@instance.expects(:puts_help).never
@instance.execute(["--version"])
end
should "just print the help by default" do
@instance.expects(:puts_version).never
@klass.expects(:puts_help)
@instance.execute([])
end