From 95592eb7f6e346115dc2171118e6efadf6e4648d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 13 Apr 2010 15:45:38 -0700 Subject: [PATCH] `vagrant --version` outputs the version --- lib/vagrant/command.rb | 4 ++-- lib/vagrant/commands/base.rb | 32 ++++++++++++++++++++++++++---- test/vagrant/commands/base_test.rb | 9 ++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index ec673b810..1f63e20fb 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -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 \ No newline at end of file diff --git a/lib/vagrant/commands/base.rb b/lib/vagrant/commands/base.rb index ed4779987..66027ef47 100644 --- a/lib/vagrant/commands/base.rb +++ b/lib/vagrant/commands/base.rb @@ -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) - # Just print out the help, since this top-level command does nothing - # on its own - self.class.puts_help + 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, diff --git a/test/vagrant/commands/base_test.rb b/test/vagrant/commands/base_test.rb index 5f0cba117..65f056faf 100644 --- a/test/vagrant/commands/base_test.rb +++ b/test/vagrant/commands/base_test.rb @@ -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