valibuk.net

A place for opinions and notes of a valibuk.

Command Line Options Parsing in Ruby

Tagged with: — ondrej at 3:35 pm on Friday, April 2, 2010

One of the most common ways how to control a command line application is to use options, e.g. almost every command line application would understand -h or --help or /? (common in the MS-DOS world) as a request to show some instruction.

Ruby (but also other programming languages such as Java) has a basic support for accessing command line arguments, so there is a space for libraries that make reading options easier such as Trollop, Optiflag or Choice.
Update: Actually not so true for Ruby – there are two standard libraries (they are built-in, so it is not necessary to install them): OptionParser and GetoptLong. Check them out. (thnx to Max)

The Choice library is an interesting choice, not only because it works smoothly, but it is a nice example of a domain specific language (DSL or see my older post about DSL in Ruby).

(There is more where this came from … )