如何在没有无效选项警告的情况下为Vagrant实现自定义选项?

ken*_*orb 2 ruby getopt rescue vagrant vagrantfile

我正在尝试为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
Run Code Online (Sandbox Code Playgroud)

现在,每当我运行任何流浪汉命令时,它都会显示以下警告,例如

vagrant destroy -f
Run Code Online (Sandbox Code Playgroud)

/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'...
Run Code Online (Sandbox Code Playgroud)

有什么方法可以忽略上述rescue部分发生的这种警告吗?


这篇文章是类似的,但在这种情况下它没有多大帮助.

Ily*_*sky 5

这是不可能的Vagrantfile.Vagrant在加载前解析选项Vagrantfile.Vagrantfile执行该时刻ensure后,由于命令行中的自定义选项而发生异常后,Vagrant进程已经在块中.没有人可以Vagrantfile从中恢复.