gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:19:43 25 4
gpt4 key购买 nike

我正在尝试为 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 命令时,它都会显示以下警告,例如

vagrant destroy -f

/opt/vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant: invalid option -- 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是相似的,但在这种情况下并没有多大帮助。

最佳答案

Vagrantfile 中不可能做到这一点。 Vagrant 在加载 Vagrantfile 之前解析选项。在 Vagrantfile 执行的那一刻,Vagrant 进程已经在 ensure block 中,因为命令行中的自定义选项发生了异常。在 Vagrantfile 中无法从中恢复。

关于ruby - 如何在没有无效选项警告的情况下为 Vagrant 实现自定义选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38234133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com