Building an Easy-to-Use Command-Line Interface

If you’ve done a lot of shell scripting (or even written a command-line tool in C), you’re probably familiar with getopt,[14] which is a C library for parsing the command line and an obvious choice as a tool for creating your interface. Although Ruby includes a wrapper for getopt, you shouldn’t use it, because there’s a better built-in option: OptionParser. As you’ll see, OptionParser is not only easy to use but is much more sophisticated than getopt and will result in a superior command-line interface for your app. OptionParser code is also easy to read and modify, making enhancements to your app simple to implement.

Before we see how to use OptionParser, let’s first consider the input our application ...

Get Build Awesome Command-Line Applications in Ruby 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.