在 Vagrantfile 中获取 vagrant 命令行参数 [英] getting the vagrant command line arguments inside the Vagrantfile
问题描述
我有以下配置只对 vagrant up 命令有意义:
I have the following configuration that only makes sense for vagrant up command:
config.vm.provider :virtualbox do |vb|
vb.gui = false
if ENV["VB_GUI"] == "true" then vb.gui = true
else
puts("[info] VB_GUI environment variable not set so running headless")
end
end
是否可以挂钩 vagrant API 来检索当前正在执行的命令?例如
Is it possible to hook into the vagrant API to retrieve the command currently being executed? E.g.
config.vm.provider :virtualbox do |vb|
vb.gui = false
if VAGRANT_API.command == "up" # how can I do this?
if ENV["VB_GUI"] == "true" then vb.gui = true
else
puts("[info] VB_GUI environment variable not set so running headless")
end
end
end
推荐答案
Vagrantfile
只是 ruby 代码,因此您可以使用 ARGV
数组轻松获取命令行参数.
A Vagrantfile
is just ruby code so you can easily get the command line arguments using the ARGV
array.
以下面的 vagrant 命令为例:
Take the following vagrant command for example:
vagrant up webserver
这将启动在您的 Vagrantfile
中定义为网络服务器的 Vagrant 框.然后您可以像这样访问参数:
That would start the Vagrant box defined as webserver in your Vagrantfile
. You can then access the arguments like so:
ARGV[0] = up
ARGV[1] = webserver
因此,使用您的示例,您需要执行以下操作:
So using your example you need to do the following:
config.vm.provider :virtualbox do |vb|
vb.gui = false
if ARGV[0] == "up"
if ENV["VB_GUI"] == "true" then vb.gui = true
else
puts("[info] VB_GUI environment variable not set so running headless")
end
end
end
这篇关于在 Vagrantfile 中获取 vagrant 命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!