如何向 rake 任务传递多个参数

发布于 2024-09-16 10:40:52 字数 122 浏览 5 评论 0原文

我想传递多个参数,但我不知道数字。比如型号名称。如何将这些参数传递到 rake 任务中以及如何在 rake 任务中访问这些参数。

例如,$ rake test_rake_task[par1, par2, par3]

I want to pass multiple parameter but I don't know the numbers. Such as model names. How do I pass those parameters into a rake task and how do I access those parameters inside the rake task.

Like, $ rake test_rake_task[par1, par2, par3]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

铜锣湾横着走 2024-09-23 10:40:52

您可以使用 args.extras 迭代所有参数,而无需明确说明您有多少个参数。

示例:

desc "Bring it on, parameters!"
task :infinite_parameters do |task, args| 
    puts args.extras.count
    args.extras.each do |params|
        puts params
    end         
end

运行:

rake infinite_parameters['The','World','Is','Just','Awesome','Boomdeyada']

输出:

6
The
World
Is
Just
Awesome
Boomdeyada

You can use args.extras to iterate over all the arguments without explicitly stating how many parameters you have.

Example:

desc "Bring it on, parameters!"
task :infinite_parameters do |task, args| 
    puts args.extras.count
    args.extras.each do |params|
        puts params
    end         
end

To run:

rake infinite_parameters['The','World','Is','Just','Awesome','Boomdeyada']

Output:

6
The
World
Is
Just
Awesome
Boomdeyada
北渚 2024-09-23 10:40:52

Rake 支持使用数组将参数直接传递给任务,而无需使用 ENV hack。

像这样定义您的任务:

task :my_task, [:first_param, :second_param] do |t, args|
  puts args[:first_param]
  puts args[:second_param]
end

并像这样调用它:

rake my_task[Hello,World]
=> Hello
   World

帕特里克·里根 (Patrick Reagan) 在 Viget 博客上发表的这篇文章很好地解释了这一点

Rake supports passing parameters directly to a task using an array, without using the ENV hack.

Define your task like this:

task :my_task, [:first_param, :second_param] do |t, args|
  puts args[:first_param]
  puts args[:second_param]
end

And call it like this:

rake my_task[Hello,World]
=> Hello
   World

This article by Patrick Reagan on the Viget blog explains it nicely

难得心□动 2024-09-23 10:40:52

你可以尝试这样的事情:

rake test_rake_task SOME_PARAM=value1,value2,value3

在 rake 任务中:

values = ENV['SOME_PARAM'].split(',')

You may try something like that:

rake test_rake_task SOME_PARAM=value1,value2,value3

And in rake task:

values = ENV['SOME_PARAM'].split(',')
江城子 2024-09-23 10:40:52

在此博客文章中找到此示例 语法看起来更清晰一些。

例如,如果您有一个 say_hello 任务,您可以使用任意数量的参数来调用它,如下所示:

$ rake say_hello Earth Mars Venus

它是这样工作的:

task :say_hello do
  # ARGV contains the name of the rake task and all of the arguments.
  # Remove/shift the first element, i.e. the task name.
  ARGV.shift

  # Use the arguments
  puts 'Hello arguments:', ARGV

  # By default, rake considers each 'argument' to be the name of an actual task. 
  # It will try to invoke each one as a task.  By dynamically defining a dummy
  # task for every argument, we can prevent an exception from being thrown
  # when rake inevitably doesn't find a defined task with that name.
  ARGV.each do |arg|
    task arg.to_sym do ; end
  end

end

Found this example on this blog post and the syntax seems a bit cleaner.

For example, if you had a say_hello task, you could call it with any number of arguments, like so:

$ rake say_hello Earth Mars Venus

This is how it works:

task :say_hello do
  # ARGV contains the name of the rake task and all of the arguments.
  # Remove/shift the first element, i.e. the task name.
  ARGV.shift

  # Use the arguments
  puts 'Hello arguments:', ARGV

  # By default, rake considers each 'argument' to be the name of an actual task. 
  # It will try to invoke each one as a task.  By dynamically defining a dummy
  # task for every argument, we can prevent an exception from being thrown
  # when rake inevitably doesn't find a defined task with that name.
  ARGV.each do |arg|
    task arg.to_sym do ; end
  end

end
断肠人 2024-09-23 10:40:52

使用args.values。

task :events, 1000.times.map { |i| "arg#{i}".to_sym } => :environment do |t, args|
  Foo.use(args.values)
end

Use args.values.

task :events, 1000.times.map { |i| "arg#{i}".to_sym } => :environment do |t, args|
  Foo.use(args.values)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文