0

Is there a way to get the command line option when executing the command task?

for eg.

cap staging namespace:task -z target_host

I'll try command-line arguments(ARGV) but length is only 2 that's for ["staging", "namespace:task"] I want to get -z (--HOST option).

2 Answers2

1

It depends on your version of Capistrano.

In the case you're using the version 3, here is blog article explaining it: https://jtway.co/capistrano-3-passing-parameters-to-your-task-e22cc9f659c3.

Basically you pass arguments like you would do with rake tasks, so passing them between brackets ([]):

cap staging mynamespace:mytask[argument1,argument2]

Then in your task, you can catch them like so:

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

In your example you would have to execute Capistrano that way:

cap staging namespace:task[target_host] 

First argument would be the target hostname.

ZedTuX
  • 2,859
  • 3
  • 28
  • 58
  • Thank you for your response sir, how about cap command options? i need to get this -z, --hosts HOSTS – mike watawski Nov 12 '19 at 07:41
  • You cannot use flags with Capistrano, you must pass then as arguments. I'm updating my answer in order to cover your example. – ZedTuX Nov 12 '19 at 09:02
0

Reference:

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