kenorb kenorb - 1 year ago 186
Ruby Question

How to implement custom option for Vagrant without having invalid option warning?

I'm trying to implement new custom option for Vagrant as in the following


# -*- mode: ruby -*-
require 'getoptlong'

opts =
[ '--vm-name', GetoptLong::OPTIONAL_ARGUMENT ],

vm_name = ENV['VM_NAME'] || 'default'

opts.each do |opt, arg|
case opt
when '--vm-name'; vm_name = arg
# @fixme: An invalid option error happens here.

Vagrant.configure(2) do |config|
config.vm.define vm_name
config.vm.provider "virtualbox" do |vbox, override| = "ubuntu/wily64"

Now, each time when I run any vagrant command it's showing the following warning, e.g.

vagrant destroy -f

/opt/vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant: invalid option -- f

Another example:

$ vagrant --vm-name=foo up --no-provision
/opt/vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant: unrecognized option `--no-provision'
Bringing machine 'foo' up with 'virtualbox' provider...
==> foo: Importing base box 'ubuntu/wily64'...

Is there any way that I can ignore such warning from happening in the above

This post is similar, but it doesn't help much in this case.

Answer Source

It's impossible to do this in Vagrantfile. Vagrant parses options before loading Vagrantfile. The moment Vagrantfile is executed, Vagrant process is already in the ensure block after the exception that occurred because of the custom option in the command line. There is nothing one can do in Vagrantfile to recover from that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download