如何将命令行参数传递给rake任务 [英] How to pass command line arguments to a rake task

查看:272
本文介绍了如何将命令行参数传递给rake任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个rake任务需要插入一个值到多个数据库。



我想从命令行或从另一个 rake任务将此值传递到rake任务。



我如何做到这一点?

解决方案

选项和依赖需要在数组内。 ..

  namespace:thing do 
descit do a thing
task:work, :option,:foo,:bar] => [:environment] do | t,args |
putswork,args
end

task:another,[:option,:foo,:bar] => [:environment] do | t,args |
putsanother#{args}
Rake :: Task [thing:work] invoke(args [:option],args [:foo],args [:bar])
#或splat args
#Rake :: Task [thing:work]。invoke(* args)
end

end



然后

 工作[1,2,3] 
=> work:{:option =>1,:foo =>2,:bar =>3}

rake:another [1,2,3]
=>另一个{:option =>1,:foo =>2,:bar =>3}
=> work:{:option =>1,:foo =>2,:bar =>3}


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?

解决方案

options and dependencies need to be inside arrays...

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

  task :another, [:option, :foo, :bar] => [:environment] do |t, 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"}

这篇关于如何将命令行参数传递给rake任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆