Command-Line-Interfaces (CLI) using Ruby

Command-Line-Interfaces are a fast way to provide an interface for a script that you use frequently or wish to share with others. In the Ruby ecosystem, many options exist, but to get started, you can look at the built-in OptionParser library.

Here is a simple example:

An unfortunate downside is the lack of a “forced” input argument for OptionParser. OptionParser’s definition of required argument means that if a switched is used, then it must have an argument; it does not mean the switch must be used.

To overcome that, and to provide a more sophisticated OO example, you can refer to the example below.

There you have it. A basic CLI for your scripts. Cheers!