Commands specify their option parsings in the `options_spec` method. Parent takes care of the rest.
This commit is contained in:
parent
26c12a9a17
commit
b4d1ee6e83
|
@ -84,17 +84,31 @@ module Vagrant
|
||||||
self.class.puts_help
|
self.class.puts_help
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse options out of the command-line. This method uses `optparse`
|
# This method is called by the base class to get the `optparse` configuration
|
||||||
# to parse command line options. A block is required and will yield
|
# for the command.
|
||||||
# the `OptionParser` object along with a hash which can be used to
|
def options_spec(opts)
|
||||||
# store options and which will be returned as a result of the function.
|
opts.banner = "Usage: vagrant SUBCOMMAND"
|
||||||
def parse_options(args)
|
end
|
||||||
options = {}
|
|
||||||
@parser = OptionParser.new do |opts|
|
|
||||||
yield opts, options
|
|
||||||
end
|
|
||||||
|
|
||||||
@parser.parse!(args)
|
# Returns the `OptionParser` instance to be used with this subcommand,
|
||||||
|
# based on the specs defined in {#options_spec}.
|
||||||
|
def option_parser(reload=false)
|
||||||
|
@option_parser = nil if reload
|
||||||
|
@option_parser ||= OptionParser.new do |opts|
|
||||||
|
options_spec(opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# The options for the given command. This will just be an empty hash
|
||||||
|
# until {#parse_options} is called.
|
||||||
|
def options
|
||||||
|
@options ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
|
# Parse options out of the command-line. This method uses `optparse`
|
||||||
|
# to parse command line options.
|
||||||
|
def parse_options(args)
|
||||||
|
option_parser.parse!(args)
|
||||||
options
|
options
|
||||||
rescue OptionParser::InvalidOption
|
rescue OptionParser::InvalidOption
|
||||||
show_help
|
show_help
|
||||||
|
@ -114,7 +128,7 @@ module Vagrant
|
||||||
puts "Description: #{description}"
|
puts "Description: #{description}"
|
||||||
end
|
end
|
||||||
|
|
||||||
puts @parser.help
|
puts option_parser.help
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,13 +5,13 @@ module Vagrant
|
||||||
description "Initializes current folder for Vagrant usage"
|
description "Initializes current folder for Vagrant usage"
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
parse_options(args) do |opts, options|
|
|
||||||
opts.banner = "Usage: vagrant init [name]"
|
|
||||||
end
|
|
||||||
|
|
||||||
create_vagrantfile(args[0])
|
create_vagrantfile(args[0])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def options_spec(opts)
|
||||||
|
opts.banner = "Usage: vagrant init [name]"
|
||||||
|
end
|
||||||
|
|
||||||
# Actually writes the initial Vagrantfile to the current working directory.
|
# Actually writes the initial Vagrantfile to the current working directory.
|
||||||
# The Vagrantfile will contain the base box configuration specified, or
|
# The Vagrantfile will contain the base box configuration specified, or
|
||||||
# will just use "base" if none is specified.
|
# will just use "base" if none is specified.
|
||||||
|
|
|
@ -71,36 +71,40 @@ class CommandsBaseTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "getting the option parser" do
|
||||||
|
should "create it with the options spec if it hasn't been created yet" do
|
||||||
|
opts = mock("opts")
|
||||||
|
result = mock("result")
|
||||||
|
OptionParser.expects(:new).yields(opts).returns(result)
|
||||||
|
@instance.expects(:options_spec).with(opts)
|
||||||
|
|
||||||
|
assert_equal result, @instance.option_parser(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not create it once its been created" do
|
||||||
|
result = mock("result")
|
||||||
|
OptionParser.expects(:new).once.returns(result)
|
||||||
|
|
||||||
|
assert_equal result, @instance.option_parser(true)
|
||||||
|
assert_equal result, @instance.option_parser
|
||||||
|
assert_equal result, @instance.option_parser
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "parsing options" do
|
context "parsing options" do
|
||||||
setup do
|
setup do
|
||||||
@args = []
|
@args = []
|
||||||
|
|
||||||
|
@options = mock("options")
|
||||||
|
@option_parser = mock("option_parser")
|
||||||
|
|
||||||
|
@instance.stubs(:option_parser).returns(@option_parser)
|
||||||
|
@instance.stubs(:options).returns(@options)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "return the options hash" do
|
should "parse the options with the args" do
|
||||||
value = mock("foo")
|
@option_parser.expects(:parse!).with(@args).once
|
||||||
result = @instance.parse_options(@args) do |opts, options|
|
assert_equal @options, @instance.parse_options(@args)
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue