Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake "variadic" task

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!

like image 506
Yossi Avatar asked Dec 22 '25 14:12

Yossi


1 Answers

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)

like image 92
Markus Avatar answered Dec 24 '25 06:12

Markus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!