3

I need to define a rake task that can handle any amount of arguments, pretty much like variadic functions in C++. But I could not find a way to define it within rake's DSL.

How can I create a rake task which can handle any amount of arguments? An example will be useful.

Thanks!

Yossi
  • 11,778
  • 2
  • 53
  • 66

2 Answers2

2

Rake as of version 0.8 cannot handle this, as mentioned in its documentation (it simply ignores any additional arguments).

However, if you can switch to using Thor, you can get this behavior. To demonstrate, create a Thorfile with this content:

class VariableArguments < Thor
  desc 'multiple [ARGS]', "Task with multiple arguments"
  def multiple(*args)
    p args
  end
end

and then call it like this:

$ thor variable_arguments:multiple one two three four five six

This prints the following:

["one", "two", "three", "four", "five", "six"]

(Tested using Thor 0.14.6)

Markus
  • 526
  • 3
  • 5
0

Building on the post here: How do I pass command line arguments to a rake task I would think that the arg1 would simply be an array that you could do a array.each do |arg|. From the command line it would then be a matter of them passing in a quoted, comma-delimited list.

Community
  • 1
  • 1
ScottJShea
  • 7,041
  • 11
  • 44
  • 67
  • Yeah I was digging into some articles and arguments need to be passed individually and the task needs to know the specific arguments ahead of time. The only way anyone got an array in was to define it ahead of time and then account for that in the code. – ScottJShea Nov 17 '11 at 16:27