Ruby CLI tool: OptionParser and clean exit codes
9 steps · about 2 h 55 min · Intermediate
- Command-line interfaces with Ruby OptionParser (blog, Koen Woortman, 15 min)
A gentle first look at creating a parser, adding flags and generating help text, with optparse shipping with Ruby.
- tutorial - Documentation for Ruby 3.4 (doc, Ruby documentation, 40 min)
The official tutorial covers option names, argument types, patterns and error behaviour in one place.
- Command Line Apps with OptionParse (article, SitePoint, 20 min)
Walks through a small working script so you see parse! and ARGV handling in a whole program.
- Parsing Command Line Arguments with OptionParser (guide, Mike Slinn, 20 min)
Shows how to validate positional parameters left in ARGV, such as a mandatory filename, once options are parsed.
- class OptionParser - Documentation for Ruby 3.4 (reference, Ruby documentation, 30 min)
The API reference for looking up details once you have built a first tool.
- Exit in Ruby - Learn X By Example (tutorial, Learn X By Example, 10 min)
A short runnable example of exiting with a chosen status and then checking it from the shell.
- Exit, Exit! Abort, Raise...Get Me Outta Here! (article, SitePoint, 15 min)
Compares exit, abort and raise and shows at_exit handlers, to help you pick the right way out for each error.
- Module: Process (Ruby 2.6) (reference, Ruby documentation, 10 min)
Gives the exact semantics of exit, abort and at_exit, including that SystemExit can be rescued.
- Graceful Ruby Exits (blog, Unfounded Labs, 15 min)
Finishes with cleanup on every exit path, including Ctrl-C and reaching the end of the script, using at_exit.