Capstone: todo CLI

Contents
The spec
OptionParser
JSON file store
Minitest for the store

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

Next: Capstone: JSON API

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: