This is a list of Ruby Gems to simplify parsing of command lines and managing text output.

Generic Links:

Tools I have looked at:

  • OptParse is the Swiss-army knife of options for Ruby, but it is a bit low-level. Look at Subcommands with OptParse 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 pwss
  • 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
    • the 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 scaffold 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 bin directory.

As of writing, Slop 31 seems to be by far the most popular option, with more than 10M downloads on Ruby Toolbox.

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 Rake
  1. It has to be mentioned that version 4 has been only recently released