Capstone: todo CLI
The spec
The first capstone is a command line todo manager backed by one JSON file. Three commands cover the whole behavior: add a task, list tasks, and mark a task done.
Everything uses only the Ruby standard library, so the project runs anywhere Ruby runs.
ruby todo.rb --add "Buy milk"
ruby todo.rb --list
ruby todo.rb --done 1
OptionParser
OptionParser turns flags into a plain options hash with help text included. Describe each flag once with names, a placeholder, and a short label.
Parse with a bang method so processed flags leave ARGV and the rest of the program sees only plain arguments.
require "optparse" options = {} OptionParser.new do |opts| opts.on("-a", "--add TITLE", "Add a todo") do |v| options[:add] = v end opts.on("-l", "--list", "List todos") { options[:list] = true } end.parse! puts options.inspect
JSON file store
The store module hides all file details behind read and write methods. Read returns an empty array when the file does not exist yet.
Pretty generate keeps the file human readable, which makes debugging the CLI far easier.
require "json" module Store FILE = "todos.json" def self.read return [] unless File.exist?(FILE) JSON.parse(File.read(FILE)) end def self.write(items) File.write(FILE, JSON.pretty_generate(items)) end end
Minitest for the store
Test the store before wiring the CLI around it. One round trip test writes an item and reads it back with an equality check.
Run the test file directly and minitest reports pass or fail with a short summary line.
require "minitest/autorun" require_relative "store" class TestStore < Minitest::Test def test_write_and_read Store.write([{ "title" => "x" }]) assert_equal("x", Store.read.first["title"]) end end
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
parse!do to the arguments it reads?Answer
It removes them from ARGV, so the rest of the program sees only plain arguments
The bang method mutates the array it parses, which is why the store test and the dispatch read a clean list afterwards.
-
What does this program print?
require "optparse" options = {} OptionParser.new { |o| o.on("-l", "--list") { options[:list] = true } }.parse!(["--list"]) p optionsAnswer
{list: true}
The callback stores a boolean under a symbol, and p prints the hash in the newer symbol-key form.
-
What does this program print?
require "json" puts JSON.pretty_generate([{ "title" => "x" }])Answer
[ { "title": "x" } ]
The pretty form writes one key per line, which is what makes the JSON file readable while debugging.
-
What does this program print?
require "optparse" argv = ["--list"] OptionParser.new { |o| o.on("--list") { } }.parse!(argv) p argvAnswer
[]
The only argument is a flag the parser knows, so nothing is left for the program.
-
What does
Store.readreturn when the JSON file does not exist yet?Answer
An empty array
The guard returns an empty list, so the first
--listof a new project works. -
What does this program print?
items = [{ "title" => "a", "done" => false }] items[0]["done"] = true p items[0]["done"]Answer
true
The done command flips the flag on the stored item before the write.
-
Why does the store use
JSON.pretty_generaterather thanJSON.generate?Answer
The file stays human readable, which makes debugging the CLI easier
The compact form is right for a wire, and a file a person may open is better with one key per line.
-
What does this program print?
require "json" data = JSON.parse('[{"title":"a","done":false}]') puts data.first["title"]Answer
a
The first element is a hash and the key reads its title, which is what the list command prints.
-
What does the Minitest test of the store do in one sentence?
Answer
It writes an item, reads it back, and compares the title
One round trip is enough to prove that the write and the read agree on the format.
-
What does this program print?
options = { add: "milk" } puts options.inspectAnswer
{add: "milk"}
inspect writes the hash in the symbol-key form Ruby 3.4 uses, and puts prints that text.
Article author: Arthur Isaev