diff --git a/lib/vagrant.rb b/lib/vagrant.rb index d359bf0fe..33df916c8 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -94,6 +94,7 @@ end I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root) # Register the built-in commands +Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy } Vagrant.commands.register(:up) { Vagrant::Command::Up } # Register the built-in config keys diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index e4cff3477..6142f8beb 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -2,6 +2,7 @@ module Vagrant module Command autoload :Base, 'vagrant/command/base' + autoload :Destroy, 'vagrant/command/destroy' autoload :Up, 'vagrant/command/up' end end diff --git a/lib/vagrant/command/base.rb b/lib/vagrant/command/base.rb index 8a4498fd8..1ad566c21 100644 --- a/lib/vagrant/command/base.rb +++ b/lib/vagrant/command/base.rb @@ -31,11 +31,14 @@ module Vagrant # # If this method returns `nil`, then you should assume that help # was printed and parsing failed. - def parse_options(opts) + def parse_options(opts=nil) # Creating a shallow copy of the arguments so the OptionParser # doesn't destroy the originals. argv = @argv.dup + # Default opts to a blank optionparser if none is given + opts ||= OptionParser.new + # Add the help option, which must be on every command. opts.on_tail("-h", "--help", "Print this help") do puts opts.help diff --git a/lib/vagrant/command/destroy.rb b/lib/vagrant/command/destroy.rb index a672c0f24..d719ed06d 100644 --- a/lib/vagrant/command/destroy.rb +++ b/lib/vagrant/command/destroy.rb @@ -1,13 +1,24 @@ +require 'optparse' + module Vagrant module Command - class DestroyCommand < NamedBase - register "destroy", "Destroy the environment, deleting the created virtual machines" - + class Destroy < Base def execute - target_vms.each do |vm| + opts = OptionParser.new do |opts| + opts.banner = "Usage: vagrant destroy [vm-name]" + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + @logger.debug("'Destroy' each target VM...") + with_target_vms(argv[0]) do |vm| if vm.created? + @logger.info("Destroying: #{vm.name}") vm.destroy else + @logger.info("Not destroying #{vm.name}, since it isn't created.") vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created") end end diff --git a/test/unit/vagrant/command/base_test.rb b/test/unit/vagrant/command/base_test.rb index b3ea69b97..caa716e67 100644 --- a/test/unit/vagrant/command/base_test.rb +++ b/test/unit/vagrant/command/base_test.rb @@ -25,6 +25,11 @@ describe Vagrant::Command::Base do result.should == ["foo"] end + it "creates an option parser if none is given" do + result = klass.new(["foo"], nil).parse_options(nil) + result.should == ["foo"] + end + ["-h", "--help"].each do |help_string| it "returns nil and prints the help if '#{help_string}' is given" do instance = klass.new([help_string], nil)