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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access