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 <
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 <
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.