Tuesday, 4 July 2017

ruby - How to pass command line arguments to a rake task



I have a rake task that needs to insert a value into multiple databases.



I'd like to pass this value into the rake task from the command line, or from another rake task.



How can I do this?



Answer



options and dependencies need to be inside arrays:



namespace :thing do
desc "it does a thing"
task :work, [:option, :foo, :bar] do |task, args|
puts "work", args
end

task :another, [:option, :foo, :bar] do |task, args|

puts "another #{args}"
Rake::Task["thing:work"].invoke(args[:option], args[:foo], args[:bar])
# or splat the args
# Rake::Task["thing:work"].invoke(*args)
end

end


Then




rake thing:work[1,2,3]
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}

rake thing:another[1,2,3]
=> another {:option=>"1", :foo=>"2", :bar=>"3"}
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}




NOTE: variable task is the the task object, not very helpful unless you know/care about Rake internals.




RAILS NOTE:




If running the task from rails, its best to preload the environment by adding => [:environment] which is a way to setup dependent tasks.




  task :work, [:option, :foo, :bar] => [:environment] do |task, args|

puts "work", args
end

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...