The Art of Arguments

There is nothing easier than parsing the command-line arguments given to your Ruby program: It is an array found in the special variable $*:

$ ruby -e 'p $*' -- some command line --arguments
["some", "command", "line", "--arguments"]

That is Too Easy!

The trouble begins with supporting common arguments conventions, like GNU's, and combining it with Ruby DSLs. This has lead to hundreds of Ruby libraries parsing command line options, even the standard library includes two ways: OptParse and GetoptLong.

Which One is the Best?

Despite all the options above, I found myself using $* directly very often, because it is simple and does not require remembering the API of an option parser. Unfortunately, this also means loosing the ability to understand the mentioned conventions for command-line options.

This changed the day I came across the minimist option parser for NodeJS! It evolved out of the Optimist option parser, which has the following slogan:

With Optimist, the options are just a hash! No optstrings attached.

— node-optimist readme

We Need This in Ruby!

Why not?¹ It features a superior API:

$ ruby example/parse.rb -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  c: true,
  beep: 'boop' }

The contents of parse.rb

require 'rationalist'
p Rationalist.parse(ARGV)

¹ By the way: On its syntactical surface, JavaScript and Ruby have more in commen than you would usually expect. Take arrays [] and hashes {}: They just look very similar!
² To continue the tradition of Perl-style two-letter variables, we should probably do:
$★ = Rationalist.parse(ARGV)

More Idiosyncratic Ruby