Capistrano获取命令行选项

执行命令任务时是否可以获取命令行选项?

例如

cap staging namespace:task -z target_host

我将尝试使用命令行参数(ARGV),但长度仅为2,这是我想获取-z(-HOST选项)的[“ staging”,“ namespace:task”]。

funnydoing 回答:Capistrano获取命令行选项

这取决于您的Capistrano版本。

在使用版本3的情况下,以下是博客文章对此进行了说明:https://jtway.co/capistrano-3-passing-parameters-to-your-task-e22cc9f659c3

基本上,您像传递rake任务一样传递参数,因此在方括号([])之间传递参数:

cap staging mynamespace:mytask[argument1,argument2]

然后在您的任务中,您可以像这样捕获它们:

namespace : mynamespace do
  desc 'Description of my task here'
  task :mytask do |task,args|
    puts "Arguments: #{args.inspect}"
  end
end

在您的示例中,您将必须以这种方式执行Capistrano:

cap staging namespace:task[target_host] 

第一个参数是目标主机名。

,

参考:

https://stackoverflow.com/a/21446021/12359143 https://github.com/capistrano/capistrano/blob/master/lib/capistrano/configuration/host_filter.rb

Capistrano::Configuration::HostFilter.class_eval do

  def filter(servers)
    Array(servers).select { |s| @rex.match s.to_s }
  end

end
本文链接:https://www.f2er.com/3119666.html

大家都在问