Option parsing is in on the base command

This commit is contained in:
Mitchell Hashimoto 2010-04-13 01:42:11 -07:00
parent d8387f8280
commit 5c3171a8f9
3 changed files with 96 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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