使用多个参数 Rake 执行 [英] Rake Execute With Multiple Arguments

查看:26
本文介绍了使用多个参数 Rake 执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个任务中调用了一个 rake 任务,但在调用 execute 时遇到了障碍

I'm calling a rake task within a task and I'm running into a roadblock when it comes to calling execute

response = Rake::Task["stuff:sample"].execute[:match => "HELLO"]

response = Rake::Task["stuff:sample"].execute[:match => "HELLO",:freq=>'100']

调用任务

task :sample, [:match,:freq] => :environment  do |t, args|

我返回的错误是无法将哈希转换为整数"

The error I get back is 'can't convert Hash into Integer'

有什么想法吗?

推荐答案

执行语法中的方括号让我感到困惑.这是一种特殊的 rake 语法(您可能使用不正确)还是您的意思是发送一个包含一个元素(散列)的数组?不是和这个一样吗?

The square brackets in your execute syntax confuse me. Is that a special rake syntax (that you may be using incorrectly) or do you mean to send an array with one element (a hash)? Isn't it the same as this?

response = Rake::Task["sample"].execute([:match => "HELLO",:freq=>'100'])

除此之外,Task#execute 需要 Rake:TaskArguments.

Beyond that, Task#execute expects Rake:TaskArguments.

class TaskArguments
    ...

    # Create a TaskArgument object with a list of named arguments
    # (given by :names) and a set of associated values (given by
    # :values).  :parent is the parent argument object.
    def initialize(names, values, parent=nil)

您可以使用:

stuff_args = {:match => "HELLO", :freq => '100' }
Rake::Task["stuff:sample"].execute(Rake::TaskArguments.new(stuff_args.keys, stuff_args.values))

您也可以使用 Task#invoke,它将接收基本参数.如果您多次调用它,请确保Task#reenable.

You could also use Task#invoke, which will receive basic args. Make sure you Task#reenable if you invoke it multiple times.

这篇关于使用多个参数 Rake 执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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