Ruby Command Line Parsers
This is a list of Ruby Gems to simplify parsing of command lines and
managing text output.
Tools I have looked at:
is the Swiss-army knife of options for Ruby, but it is a
bit low-level. Look at Subcommands with
for instructions on how to set global options and subcommands.
- Slop v3 parses options and
supports subcommands. The syntax is straightforward and it is the
library I am now using for
- Slop v4, recently released,
drops support for commands, although a combination of slop option
parsers can be used to do the trick (as long as there are no options
with the same name). The idea is the following:
- A first Slop parser is used to parse the global options
arguments method is then called to get the first element
Slop could not parse, that is, the subcommand.
- Based on the the value of the previous step, other
command-specific Slop parsers are invoked to parse the options
of each specific subcommand.
- Mercenary is the library used
by Jekyll. The library parses subcommands
with options and arguments, but it does not support global options.
- Commander parses subcommands with
options and arguments and it also support global options. However it
ships with a bunch of stuff which is not always needed
(Higline and Notify)
- GLI has global options and
subcommands with options and arguments. The gem has a
command and it seems to produce very structured documentation.
Instructions on how to write code by hand, however, cannot be easily
found (this could be useful if you are migrating an
existing application). The best solution is
scaffolding an empty
application and then copy the file generated in the
As of writing, Slop 3 seems to be by far the most popular option,
with more than 10M downloads on Ruby
Other (related) Gems:
- Highline is for asking values
from the command line
- Cocaine is for building and
run commands with options
- Colorize is for coloring
output to terminals.
- Thor is an alternative to