我正在尝试为Vagrant实施新的自定义选项,如下面的Vagrantfile
所示:
# -*- mode: ruby -*-
require 'getoptlong'
opts = GetoptLong.new(
[ '--vm-name', GetoptLong::OPTIONAL_ARGUMENT ],
)
vm_name = ENV['VM_NAME'] || 'default'
begin
opts.each do |opt, arg|
case opt
when '--vm-name'; vm_name = arg
end
end
rescue
# @fixme: An invalid option error happens here.
end
Vagrant.configure(2) do |config|
config.vm.define vm_name
config.vm.provider "virtualbox" do |vbox, override|
override.vm.box = "ubuntu/wily64"
end
end
现在,每当我运行任何流浪汉命令时,它都会显示以下警告,例如
vagrant destroy -f
/opt/vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant:无效选项 - f
另一个例子:
$ 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'...
有没有办法可以忽略上述rescue
部分发生的警告?
此post类似,但在这种情况下没有多大帮助。
答案 0 :(得分:5)
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.
答案 1 :(得分:3)
我认为可以避免错误。警告 - 我是流浪汉的新手。但是,这似乎可以满足您的需求:
opts = GetoptLong.new(
[ '--vm-name', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--host-name', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--provider', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--no-provision', GetoptLong::OPTIONAL_ARGUMENT ],
)
vm_name = ENV['VM_NAME'] || 'default'
host_name = ENV['HOST_NAME'] || 'localhost.localdomain'
如果您可以预期您认为将传递给您的vagrant调用的所有命令行选项,您可以将它们添加到getopts数组中,然后忽略您希望通过默认的vagrant处理处理的元素。