Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass named arguments to a Rake task?

Tags:

Is there a way to pass named arguments to a Rake task without using environment variables?

I am aware that Rake tasks can accept arguments in two formats:

Environment Variables

$ rake my_task foo=bar 

This creates an environment variable with the name foo and the value bar that can be accessed in the Rake task my_task by ENV['foo'].

Rake Task Arguments

$ rake my_task['foo','bar'] 

This passes the values foo and bar to the first two task arguments (if they are defined). If my_task were defined as:

task :my_task, :argument_1, :argument_2 

then argument_1 would have the value foo and argument_2 would have the value bar.

like image 767
Andrew Avatar asked Feb 23 '11 01:02

Andrew


People also ask

How do I list a rake task?

You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks . Each task has a description, and should help you find the thing you need.


1 Answers

You can say things like this:

rake some_task -- --arg=value 

And then, inside your task, ARGV will be

[ 'some_task', '--arg=value' ] 

so you could use OptionParser (or some other option parser) to unpack ARGV just like in any old CLI script; the funny looking -- is necessary to keep rake from trying to parse --arg=like as a rake switch.

You're probably better off with the standard environment variable approach, it isn't as ugly as all the -- stuff and it is the usual way of passing arguments to rake tasks.

like image 143
mu is too short Avatar answered Oct 04 '22 13:10

mu is too short