A real CLI with OptionParser

The parser

OptionParser is in the standard library and does the part of a CLI that is boring: the flags, the help and the error for an unknown option. The file is todo.rb, and the command itself is a case on the first argument:

#!/usr/bin/env ruby require "json" require "optparse" FILE = "todos.json" OPTIONS = {} parser = OptionParser.new do |o| o.banner = "Usage: todo [options] COMMAND [args]" o.on("-f", "--file PATH", "todo file (default: todos.json)") { |v| OPTIONS[:file] = v } o.on("-h", "--help", "show this help") { puts o; exit } end begin parser.parse! rescue OptionParser::ParseError => e abort("#{e.message}\n#{parser.help}") end

parse! removes the flags from ARGV and leaves the command and its arguments. The rescue turns a bad flag into a message and a help text instead of a stack trace, which is the difference between a tool and a script.

Subcommands

Each subcommand reads its own arguments from what is left. The complete todo.rb below adds the parser from the section above to the dispatch, and the file it reads is JSON, loaded at the start and written after a change:

#!/usr/bin/env ruby require "json" require "optparse" FILE = "todos.json" OPTIONS = {} parser = OptionParser.new do |o| o.banner = "Usage: todo [options] COMMAND [args]" o.on("-f", "--file PATH", "todo file (default: todos.json)") { |v| OPTIONS[:file] = v } o.on("-h", "--help", "show this help") { puts o; exit } end begin parser.parse! rescue OptionParser::ParseError => e abort("#{e.message}\n#{parser.help}") end command = ARGV.shift or abort(parser.help) path = OPTIONS[:file] || FILE todos = File.exist?(path) ? JSON.parse(File.read(path)) : [] save = -> { File.write(path, JSON.pretty_generate(todos)) } case command when "add" title = ARGV.shift or abort("todo add: missing title") todos << { "id" => todos.size + 1, "title" => title, "done" => false } save.call puts "added: #{title}" when "list" todos.each { |t| puts format("%d [%s] %s", t["id"], t["done"] ? "x" : " ", t["title"]) } when "done" id = Integer(ARGV.shift) rescue abort("todo done: id must be a number") todo = todos.find { |t| t["id"] == id } or abort("todo done: no todo #{id}") todo["done"] = true save.call puts "done: #{todo["title"]}" else abort("todo: unknown command #{command.inspect}") end

abort writes to stderr and exits with status 1, which is exactly the contract a shell and a CI job expect. Every branch that cannot continue calls it with a message that names the command and what was wrong.

Running it

ruby todo.rb --help

Usage: todo [options] COMMAND [args] -f, --file PATH todo file (default: todos.json) -h, --help show this help

ruby todo.rb add "write the lesson"

added: write the lesson

ruby todo.rb list

1 [ ] write the lesson

ruby todo.rb done 1

done: write the lesson

ruby todo.rb list

1 [x] write the lesson

Errors and exit codes

The three failures a user meets, each with the message and the status:

ruby todo.rb --nope

invalid option: --nope Usage: todo [options] COMMAND [args] -f, --file PATH todo file (default: todos.json) -h, --help show this help

ruby todo.rb bogus

todo: unknown command "bogus"

ruby todo.rb add

todo add: missing title

Each of them exits 1; success exits 0. A shell reads the number, not the message, so a command that prints an error and exits 0 breaks a pipeline in the quietest possible way.

What to watch for

The order matters: parse the flags before taking the command, or todo --help is read as a command called --help. The error above is what that bug looks like.

A subcommand named after a flag is a name collision waiting to happen. Keep the commands as plain words and let every flag start with a dash, and the parser can always tell them apart.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? require "optparse" options = {} OptionParser.new do |o| o.on("-f", "--file PATH") { |v| options[:file] = v } end.parse!(["--file", "other.json"]) puts options[:file]

    Answer

    other.json

    parse! walks the arguments, calls the block of the matching flag and stores the value it was given.

  2. What does this program print? require "optparse" begin OptionParser.new { |o| o.on("-v", "--verbose") }.parse!(["--nope"]) rescue OptionParser::ParseError => e puts e.class end

    Answer

    OptionParser::InvalidOption

    An unknown flag raises a ParseError, which the CLI rescues to print the message and the help.

  3. What does this program print? argv = ["add", "milk"] command = argv.shift puts command puts argv.first

    Answer

    add / milk

    shift takes the first word as the command and leaves the rest as its arguments.

  4. What does parser.parse! change about ARGV?

    Answer

    It removes the flags it understood and leaves the command and its arguments

    The bang form consumes the flags, which is why the command is read from what remains.

  5. What does the rescue in the parser block do with an OptionParser::ParseError?

    Answer

    Prints the message and the help text, then aborts with status 1

    A bad flag becomes a message and the help instead of a stack trace, and abort makes it a failure.

  6. What status does abort("todo: unknown command") exit with?

    Answer

    1

    abort writes the message to stderr and ends the program with status 1.

  7. Why must the flags be parsed before the command is taken from ARGV?

    Answer

    Because todo --help would otherwise be read as a command named --help

    The order is what lets todo --help reach the help handler instead of the command dispatch.

  8. What does command = ARGV.shift or abort(parser.help) do when no command is given?

    Answer

    Prints the help and exits with status 1

    shift on an empty ARGV is nil, so the right side runs and the usage text goes to stderr.

  9. Which name can a flag and a subcommand never share without a collision?

    Answer

    A name that starts with a dash

    Flags start with a dash, so a plain-word command can always be told apart from one.

  10. What is the difference between printing an error and exiting 1?

    Answer

    A program that prints an error and exits 0 breaks a pipeline without any message

    A shell reads the status rather than the text, so a wrong status hides the failure from the caller.

Article author: Arthur Isaev