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

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

问题描述

我有一个需要将值插入多个数据库的 rake 任务.

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

我该怎么做?

解决方案

选项和依赖项需要在数组中:

命名空间:做的事情desc 它做了一件事"任务 :work, [:option, :foo, :bar] 做 |task, args|puts "work", args结尾任务 :another, [:option, :foo, :bar] 做 |task, args|放置另一个#{args}";Rake::Task["thing:work"].invoke(args[:option], args[:foo], args[:bar])# 或 splat the args# Rake::Task["thing:work"].invoke(*args)结尾结尾

然后

rake thing:work[1,2,3]=>工作:{:option=>1", :foo=>2", :bar=>3"}耙东西:另一个[1,2,3]=>另一个 {:option=>1", :foo=>2", :bar=>3"}=>工作:{:option=>1", :foo=>2", :bar=>3"}

<块引用>

注意:变量 task 是任务对象,除非您了解/关心 Rake 内部结构,否则不会很有帮助.

导轨注意事项:

<块引用>

如果从 Rails 运行任务,最好通过添加 => 来预加载环境.[:environment] 这是一种设置依赖任务的方法.

 任务 :work, [:option, :foo, :bar] =>[:environment] 做 |任务,参数|puts "work", args结尾

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] 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 task object, not very helpful unless you know/care about Rake internals.

RAILS NOTE:

If running the task from Rails, it's 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

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

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