Skip to content

Ruby Blender

  • Blog
  • About
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Tag Archive for 'rubygems'
21Aug09 Arguments with Trollop
ruby rubygems
0 Comments

At various points in my life, I’ve used many methods of parsing command line options, from rolling my own, to using getopt or other libraries.  Recently I came across a new one which has its own view of how to parse commandline arguments.

Trollop is, as they put it, “Yet Another Fine CommandLine Argument Parsing Library” (YAFCLAP).  It’s goal is to be as easy as possible.  You can install it as a single file in lib, or use it as a gem.  Trollop doesn’t care.  To install, you can download it from rubyforge, or use the ever popular:

gem install trollop

(you may need to prepend sudo, or run it as root).

Let’s take a look at a simple example:

require 'rubygems'
require 'trollop'
opts = Trollop::options do
opt :host, "Host to connect to", :default => "localhost"
opt :port, "jmx remote port", :default => 4444
opt :interval, "How often do we poll (in seconds)", :default => 10
opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0
end
p opts

 

The first argument to opt is the name of the option.  We don’t have to specify a switch; it’s smart enough to figure out a mapping for them.  The second is a description of the argument.  You can specify a default value and/or a type.  In this example, we’ve specified defaults.  And here it is in use:

$ ruby trollop_simple.rb
{:interval=>10, :count=>0, :help=>false, :host=>"localhost", :port=>4444}
$ ruby trollop_simple.rb -h
Error: option '-h' needs a parameter.
Try --help for help.
$ ruby trollop_simple.rb --help
Options:
--host, -h : Host to connect to (default: localhost)
--port, -p : jmx remote port (default: 4444)
--interval, -i : How often do we poll (in seconds) (default: 10)
--count, -c : How many intervals, 0 == infinite, use ^C (SIGINT) to
exit and write output (default: 0)
--help, -e: Show this message

Notice that it has set up mappings for us as well as created a nicely formatted help — though it’s a default, without a lot of information.   Let’s add a version and usage:

require 'rubygems'
require 'trollop'
opts = Trollop::options do
version < "localhost"
opt :port, "jmx remote port", :default => 4444
opt :interval, "How often do we poll (in seconds)", :default => 10
opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0
end
p opts

Now let’s see what we have:

$ ruby trollop_simple.rb --help
jmx_mon is a program to monitor a jmx bean's value over time.
It can output the data as a csv, a graph in a pdf, or both.

Usage jmx_mon [options]
where [options] are:
--host, -h : Host to connect to (default: localhost)
--port, -p : jmx remote port (default: 4444)
--interval, -i : How often do we poll (in seconds) (default: 10)
--count, -c : How many intervals, 0 == infinite, use ^C (SIGINT) to
exit and write output (default: 0)
--version, -v: Print version and exit
--help, -e: Show this message

$ ruby trollop_simple.rb -v
jmx_mon -- we be jammin' with version zz9-za aka Arthur Dent
Don't forget your towel!

We now have a nicer usage, as well as version.  Also, it will stop automagickally if we ask for help or the version.

All of the options are placed in the opts hash; let’s see what we can do with it:

require 'rubygems'
require 'trollop'

aliases = {
:MEMORY_HEAP => "Heap memory usage, multiple values",
:MEMORY_HEAP_USED => "Used heap memory"
}

opts = Trollop::options do
version < "localhost"
opt :port, "jmx remote port", :default => 4444
opt :interval, "How often do we poll (in seconds)", :default => 10
opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0
opt :aliases, "Print defined bean aliases & exit.", :default => false
opt :towel, "Don't forget this!", :default => false
end

if opts[:aliases_given]
aliases.each_pair do |key, value|
puts "#{key}: #{value}"
end
exit
end

Trollop::die :towel, "You forgot your towel. You've been eaten by a Ravenous Bugblatter Beast of Traal" unless opts[:towel]

p opts

We’ve added two more options, one for printing out aliases, and another indicating whether or not you have your towel — with deadly results should you forget.

$ ruby trollop_simple.rb -a
MEMORY_HEAP: Heap memory usage, multiple values
MEMORY_HEAP_USED: Used heap memory

$ ruby trollop_simple.rb
Error: argument --towel You forgot your towel. You've been eaten by a Ravenous Bugblatter Beast of Traal.
Try --help for help.

There’s more that you can do with trollop; the website and documentation gives more examples.  However, I’d suggest considering trollop the next time you need to parse the command line.

 

 

Enhanced by Zemanta
18Feb09 irbrc goodness
configuration programming ruby rubygems
2 Comments

Here’s my (current) .irbrc, with comments


# I am including gems in my irb session. Therefore, the next require
require 'rubygems'

# wirble is a wonderful gem which add nice features to irb
require 'wirble'

# Dr. Nic's useful helper gem. It makes it so you can say:
# foo.map_by_bar
# instead of having to say:
# foo.map{|f| f.bar}
# more to the point you can do
# people.map_by_first_and_last
# instead of
# people.map{|p| [p.first, p.last]}
require 'map_by_method'

# what_methods is a way to see which methods, when performed on an object, return a particular value:
# >> "hi".what? 'h'
# "hi".chop! == "h"
# "hi".chop == "h"
# => ["chop!", "chop"]
require 'what_methods'

# pretty print --> provides a "pretty" view of an object
require 'pp'

# auto indent irb; it's useful for when you're just experimenting
IRB.conf[:AUTO_INDENT]=true

# start wirble (with color)
Wirble.init
#Wirble.colorize

# What are the methods which belong only to an object, and not those inherited from Object
class Object
def my_methods
(self.methods - Object.methods).sort
end
end

# strip html tags from a string
class String
def strip_tags
self.gsub(/<\S[^><]*>/,"")
end
end

I’ve shown mine. Show me yours?

Here are links to the referenced rubygems:

  • what_methods
  • map_by_method
  • wirble
Enhanced by Zemanta
 
Browse Archives »
  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)
 

Recent Posts

  • Default values for Attributes
  • Arguments with Trollop
  • Database Paranoia — ActiveRecord Callbacks
  • Ruby Bindings and Scope
  • What’s #method_missing missing?

Search

Browse by Category

  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)

Browse by Tag

  • ActiveRecord
  • binding
  • Command-line interface
  • gotcha
  • irb
  • irbc
  • Languages
  • Library
  • Local variable
  • metaprogramming
  • mini-saga
  • paradigm
  • philosophy
  • programming
  • rails
  • Recursion
  • ruby
  • rubygems
  • Ruby on Rails
  • scope
  • utilities

Browse by Month

  • September 2009 (1)
  • August 2009 (4)
  • April 2009 (1)
  • February 2009 (11)
 
 
  • Blog
  • About
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed