This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
Original Post: Using Command Line Parameters w/ Rake and Capistrano
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Almost every project I work on requires a small set of both custom rake tasks and capistrano recipes. Since my models are as fat as they can be, these tasks and recipes are usually quite slim and really only involve delegation to the appropriate model method. Ok, so apparently Ryan thinks he’s great because he religiously believes in fat models – woohoo – that’s last year’s news. What I really want to talk about is that I am always needing to pass in parameters from the command line to these tasks, and everytime I struggle finding the documentation on how to properly do that.
So let’s try to figure this out once and for all.
Command line parameters in Rake
on Rake v0.7.2
This one’s easy. Options can be passed into rake by specifying key/value pairs on the rake command:
rake options:show option1=value1
These command line options are then automatically set as environment variables which can be accessed within your rake task:
12345678
namespace :optionsdo desc "Show how to read in command line options" task :showdo p "option1 is #{ENV['option1']}"endend
I don’t love overloading the environment variables collection as a command line marshaller, but hey, it is what it is and it’s easy to use.
12
rake options:show option1=value1#=> option1 is value1
Ok, easy enough. Next.
Command line parameters in Capistrano
w/ Capistrano v1.4.1
Capistrano is a little different in that you need the -s command line switch for each key/value pair and you have a different way(s) of accessing these variables within your capistrano tasks. For instance, this is the capistrano version of the rake invocation example from earlier:
cap -a options_show -s option1=value1
And you’ve got a couple of ways to get at these variables from within your capistrano task. The first is by referencing the variable name itself:
123
task :show_optionsdo p "option1 is #{opt1}"end
This is convenient, but you get a method_missing exception when you attempt to reference that variable when it hasn’t yet been set. The more reliable accessor method is by using the configuration object available to your capistrano tasks:
123
task :show_optionsdo p "option1 is #{configuration[:opt1]}"if configuration[:opt1]end
which will simply be nil if it doesn’t exist instead of choking like the previous, direct, access.
When you’re building a task library you’ll be wrapping your tasks in a Capistrano.configuration(:must_exist) block that yields the configuration object itself. So you can rename that lengthy configuration name to c if you’re the lazy type in that scenario:
1234567
Capistrano.configuration(:must_exist).load do |c| task :show_optionsdo p "option1 is #{c[:opt1]}"if c[:opt1]endend
So while this isn’t ground-breaking research here, I’ve had trouble finding concrete documentation on passing in values from the command line to Rake and Capistrano. So consider this a note to myself from the future… except that it’s from the past… to the future Ryan… which isn’t nearly as impressive.