My Life As A Blog

Ruby Tuesday #9 : Interacting with the command line

leave a comment »

Last week I thought some sort of interface to the Twitter client I’ve been building would be a good idea.  The most basic interface is the command line.  So I thought I’d start there.  There’s a library called getoptlong that parses the command line and allows you to specify the options that can be supplied.  Before hooking this into the Twitter client, I thought I’d experiment with the library.  I created a new file for this purpose.  Here’s the code:

require "getoptlong"

FRIENDS = "Friends"
PUBLIC = "Public"
USER = "User"
UPDATE = "Update"
action = PUBLIC
username = password = ""
tweet = ""

parser =
parser.set_options(["-f", "--friends_timeline",GetoptLong::NO_ARGUMENT],                    ["-t", "--user_timeline", GetoptLong::NO_ARGUMENT],                    ["-s", "--update_status", GetoptLong::REQUIRED_ARGUMENT],                    ["-u", "--username", GetoptLong::REQUIRED_ARGUMENT],                    ["-p", "--password", GetoptLong::REQUIRED_ARGUMENT]) parser.each do |option, value|   case option     when "-f"       action = FRIENDS     when "-t"       action = USER     when "-s"       action = UPDATE       tweet = value     when "-u"       username = value     when "-p"       password = value   end end puts action puts username puts password puts tweet

So, we set up the GetoptLong object (interesting naming convention) by specifying the options.  Then we loop through the supplied options.  This is fairly naive at present, so if you specify -f and -s, it’ll take the last of the options and set the action to that.  We can call this from the command line to see what the output is (the format for this is ruby Filename.rb options.)  Getting both the option and the value passed into the block is going to seem unusual if your background is C# or Java – but it’s very convenient and fairly obvious.  Worth noting that methods can return more than one value, too (the get method and its alias get_option of the getoptlong library does just that.)  For those who’d like to interact with the command line at a lower level ARGV is your friend – it’s a global constant that represents all the options supplied at the command line in an array-like structure.  Of course, if you use ARGV you’ll need to figure out how to detect what’s an argument and what’s a value.

It’s fairly trivial to modify this code to call the Twitter client.  Add a line at the top to require Twitter and then replace the four puts lines at the bottom of the code with these lines:

client = 

case action
  when PUBLIC
    puts client.download_public_timeline
  when FRIENDS
    puts client.download_friends_timeline(username, password)
  when USER
    puts client.download_user_timeline(username)
  when UPDATE
    client.update(tweet, username, password)

Now I’ve hooked the client up to the command line, a GUI would be an interesting next step.

Written by remark

June 10, 2008 at 7:57 pm

Posted in Ruby

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: