如何将args从命令行传递给rake然后传递给rspec

时间:2021-01-14 23:23:18

I'm trying to pass a couple of variables through the command line to rake to be used in rspec.

我试图通过命令行传递几个变量来rake在rspec中使用。

From what i gather i can do this to pass args into my rake task:

从我收集的内容中我可以做到这一点,将args传递给我的rake任务:

task :my_task, :arg1, :arg2 do |t, args|
  puts "Args were: #{args}"
end

but I'm using rspec so my rake task looks like this:

但我正在使用rspec,所以我的rake任务看起来像这样:

RSpec::Core::RakeTask.new(:my_task), :arg1, :arg2 do |t, args|
 puts args.arg1
 puts args.arg2
end

which doesn't work.

这不起作用。

also I havent figured out how to pass it to my rspec spec file

我还没弄明白如何将它传递给我的rspec规范文件

2 个解决方案

#1


0  

How do you call rake?

你怎么称耙?

For your example you have to call:

对于您的示例,您必须致电:

rake mytask[val1,val2]

I'm not sure about your 2nd code example. args should be a hash with keys :arg1 and :arg2. So you may use

我不确定你的第二个代码示例。 args应该是一个带键的哈希:arg1和:arg2。所以你可以使用

puts args[:arg1]

I'm in doubt if args.arg1 will work.

我怀疑args.arg1是否有效。

#2


0  

It's been a while since the original question, but I had to solve a similar problem. You don't have to change much of your initial idea:

从最初的问题开始已经有一段时间了,但我必须解决类似的问题。你不必改变你最初的想法:

RSpec::Core::RakeTask.new(:my_task, [:arg1, :arg2]) do |t, args|
 puts args.arg1
 puts args.arg2
end

Then call it e.g. via:

然后称它为例如通过:

rake my_task["hello","world"]

#1


0  

How do you call rake?

你怎么称耙?

For your example you have to call:

对于您的示例,您必须致电:

rake mytask[val1,val2]

I'm not sure about your 2nd code example. args should be a hash with keys :arg1 and :arg2. So you may use

我不确定你的第二个代码示例。 args应该是一个带键的哈希:arg1和:arg2。所以你可以使用

puts args[:arg1]

I'm in doubt if args.arg1 will work.

我怀疑args.arg1是否有效。

#2


0  

It's been a while since the original question, but I had to solve a similar problem. You don't have to change much of your initial idea:

从最初的问题开始已经有一段时间了,但我必须解决类似的问题。你不必改变你最初的想法:

RSpec::Core::RakeTask.new(:my_task, [:arg1, :arg2]) do |t, args|
 puts args.arg1
 puts args.arg2
end

Then call it e.g. via:

然后称它为例如通过:

rake my_task["hello","world"]