Capstone: todo CLI
| The spec | |
| Parse argv | |
| Plain-text store | |
| ExUnit for the store |
The spec
Build a todo tool that runs in the terminal. Four behaviors define the whole project, and everything else is detail.
- Add writes one line of text to a plain file.
- List prints every stored line with a number.
- Done removes one line by its number.
- Missing files and bad numbers report a clear error.
Parse argv
The entry point receives raw command line arguments as a list of strings. Parse them into options and positional args with the standard parser, then match on the result.
{opts, args, _} = OptionParser.parse(argv, switches: [all: :boolean])
Keep parsing thin. Turn argv into a small command value in one place, and let the rest of the program work with that value only.
Plain-text store
One line per todo in a plain text file is enough storage for this project. The file module reads and writes the whole file, which keeps the code short and the format easy to inspect.
path = "todos.txt"
File.write!(path, "buy milk\n")
items = File.read!(path)
ExUnit for the store
Test the store, not the terminal. Adding then listing covers the core round trip, and a second test covers the empty file edge.
test "saves and lists" do
Store.add("milk")
assert "milk" in Store.list()
end
Next: Capstone: HTTP server
Article author: Arthur Isaev