如何将命令行参数传递给rake任务我有一个rake任务,需要将一个值插入到多个数据库中。我希望将这个值从命令行传递到rake任务,或者从另一个耙任务。我该怎么做?
3 回答
慕姐8265434
TA贡献1813条经验 获得超2个赞
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) endend
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"}
注:可变 task
是任务对象,除非您了解/关心Rake内部组件,否则不会很有帮助。
Rails注:
如果从Rails运行任务,最好通过添加 => [:environment]
这是一种设置 相依任务。
task :work, [:option, :foo, :bar] => [:environment] do |task, args| puts "work", args end
心有法竹
TA贡献1866条经验 获得超5个赞
require 'rake'task :my_task, [:arg1, :arg2] do |t, args| puts "Args were: #{args}"endtask :invoke_my_task do Rake.application.invoke_task("my_task[1, 2]")end# or if you prefer this syntax...task :invoke_my_task_2 do Rake::Task[:my_task].invoke(3, 4)end# a task with prerequisites passes its # arguments to it prerequisitestask :with_prerequisite, [:arg1, :arg2] => :my_task #<- name of prerequisite task# to specify default values, # we take advantage of args being a Rake:: TaskArguments objecttask :with_defaults, :arg1, :arg2 do |t, args| args.with_defaults(:arg1 => :default_1, :arg2 => :default_2) puts "Args with defaults were: #{args}"end
> rake my_task[1,2] Args were: {:arg1=>"1", :arg2=>"2"} > rake "my_task[1, 2]" Args were: {:arg1=>"1", :arg2=>"2"} > rake invoke_my_task Args were: {:arg1=>"1", :arg2=>"2"} > rake invoke_my_task_2 Args were: {:arg1=>3, :arg2=>4} > rake with_prerequisite[5,6] Args were: {:arg1=>"5", :arg2=>"6"} > rake with_defaults Args with defaults were: {:arg1=>:default_1, :arg2=>:default_2} > rake with_defaults['x','y'] Args with defaults were: {:arg1=>"x", :arg2=>"y"}
task :t1 => "dep[1,2]"
:invoke_my_task
:invoke_my_task_2
.
rake my_task\['arg1'\]
喵喵时光机
TA贡献1846条经验 获得超7个赞
ENV
rake
rake mytask var=foo
p ENV['var'] # => "foo"
- 3 回答
- 0 关注
- 658 浏览
添加回答
举报
0/150
提交
取消