Prevent Vagrant Commands from Operating on All VMs
For small projects that use Vagrant it isn't all that important that vagrant up, vagrant destroy, and other similar commands affect all defined VMs in a project in the default case where no VM name is provided. In most cases there is only the one VM, after all, and it is convenient not have to have to continually enter its name.
Not everyone uses small projects, however. What if you are working with a large project ecosystem set up such that many shared VMs are defined in one central Vagrantfile? In that sort of system the Vagrant default behavior can prove to be a real pain: you will constantly forget to specify a VM name, and then be forced into a hurried cancellation of whatever operation is about to affect all of your numerous VMs. It is very aggravating to accidentally destroy a VM that contains an experiment in the midst of completion, or that will require fifteen minutes to provision from scratch.
Fortunately it is easy enough to fix this problem. A Vagrantfile is just Ruby code, so you can add a few lines at the head in order to check on the arguments provided and exit with an error if no VM name is specified. The top of your Vagrantfile might look a little like this:
# -*- mode: ruby -*- # vi: set ft=ruby : # ------------------------------------------------------------- # Exit if no VM name specified. # ------------------------------------------------------------- commandsToCheck = [ "destroy", "halt", "provision", "reload", "resume", "suspend", "up" ] enteredCommand = ARGV[0] # Is this one of the problem commands? if commandsToCheck.include?(enteredCommand) # Is this command lacking any other arguments? e.g. "vagrant destroy". if ARGV.length == 1 puts "You must use 'vagrant #{ARGV[0]} <name>, [<name>] ...'." puts "Run 'vagrant status' to view VM names." exit 1 end end # ------------------------------------------------------------- # On with Vagrant. # ------------------------------------------------------------- # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # ... and the rest of your Vagrantfile.