From 5c3171a8f9ff82e9e6565790f270b50e6d3d6521 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 13 Apr 2010 01:42:11 -0700 Subject: [PATCH] Option parsing is in on the base command --- lib/vagrant/commands/base.rb | 30 ++++++++++++++ lib/vagrant/commands/init.rb | 4 +- test/vagrant/commands/base_test.rb | 63 ++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 test/vagrant/commands/base_test.rb diff --git a/lib/vagrant/commands/base.rb b/lib/vagrant/commands/base.rb index 47b6c9f5a..51a9a24da 100644 --- a/lib/vagrant/commands/base.rb +++ b/lib/vagrant/commands/base.rb @@ -1,3 +1,5 @@ +require 'optparse' + module Vagrant class Commands # This is the base command class which all sub-commands must @@ -9,8 +11,36 @@ module Vagrant @env = env end + # This method should be overriden by subclasses. This is the method + # which is called by {Vagrant::Command} when a command is being + # executed. The `args` parameter is an array of parameters to the + # command (similar to ARGV) def execute(args) + raise "Subcommands should implement the execute method properly." + end + # Parse options out of the command-line. This method uses `optparse` + # to parse command line options. A block is required and will yield + # the `OptionParser` object along with a hash which can be used to + # store options and which will be returned as a result of the function. + def parse_options(args) + options = {} + @parser = OptionParser.new do |opts| + yield opts, options + end + + @parser.parse!(args) + options + rescue OptionParser::InvalidOption + show_help + end + + # Prints the help for the given command. Prior to calling this method, + # {#parse_options} must be called or a nilerror will be raised. This + # is by design. + def show_help + puts @parser.help + exit end end end diff --git a/lib/vagrant/commands/init.rb b/lib/vagrant/commands/init.rb index 31c0e2126..476eb0dbb 100644 --- a/lib/vagrant/commands/init.rb +++ b/lib/vagrant/commands/init.rb @@ -2,7 +2,9 @@ module Vagrant class Commands class Init < Base def execute(args) - p args + parse_options(args) do |opts, options| + opts.banner = "Usage: vagrant init [name]" + end end end end diff --git a/test/vagrant/commands/base_test.rb b/test/vagrant/commands/base_test.rb new file mode 100644 index 000000000..347a3d958 --- /dev/null +++ b/test/vagrant/commands/base_test.rb @@ -0,0 +1,63 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') + +class CommandsBastTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Commands::Base + end + + context "initializing" do + should "setup the env attribute" do + env = mock("env") + instance = @klass.new(env) + assert_equal env, instance.env + end + end + + context "instance methods" do + setup do + @env = mock_environment + @instance = @klass.new(@env) + end + + context "executing" do + should "raise an error if called (since not a subclass)" do + assert_raises(RuntimeError) { + @instance.execute([]) + } + end + end + + context "parsing options" do + setup do + @args = [] + end + + should "return the options hash" do + value = mock("foo") + result = @instance.parse_options(@args) do |opts, options| + options[:foo] = value + end + + assert_equal value, result[:foo] + end + + should "parse with the given args" do + parser = mock("parser") + + OptionParser.stubs(:new).returns(parser) + parser.expects(:parse!).with(@args) + @instance.parse_options(@args) do; end + end + + should "show help if an invalid options error is raised" do + parser = mock("parser") + + OptionParser.stubs(:new).returns(parser) + parser.expects(:parse!).raises(OptionParser::InvalidOption) + @instance.expects(:show_help).once + + @instance.parse_options(@args) do; end + end + end + end +end