The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Using Command Line Parameters w/ Rake and Capistrano

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
Using Command Line Parameters w/ Rake and Capistrano Posted: Jun 21, 2007 9:12 PM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

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:

1
2
3
4
5
6
7
8
namespace :options do

  desc "Show how to read in command line options"
  task :show do
    p "option1 is #{ENV['option1']}"
  end

end

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.

1
2
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:

1
2
3
task :show_options do
  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:

1
2
3
task :show_options do
  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:

1
2
3
4
5
6
7
Capistrano.configuration(:must_exist).load do |c|

  task :show_options do
    p "option1 is #{c[:opt1]}" if c[:opt1]
  end

end

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.

tags: ruby, rubyonrails, rake, capistrano

Read: Using Command Line Parameters w/ Rake and Capistrano

Topic: InfoEther Previous Topic   Next Topic Topic: Visual Ruby - One Small Step...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use