Capstone: todo CLI
A command-line program is the smallest complete application: it reads the arguments, does one job and exits with a status. This capstone builds one with mix escript.build, which packs the compiled beams and the Erlang runtime into one executable file that runs without Elixir installed.
The project and the escript
The generator writes the project. The one key that turns it into an escript is escript: [main_module: Todo.CLI] in the project list of mix.exs. The key is escript: [main_module: Todo.CLI] inside project/0, and a sed command adds it to the generated list:
mix new todo
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/todo.ex
* creating test
* creating test/test_helper.exs
* creating test/todo_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd todo
mix test
Run "mix help" for more commands.
The CLI module
An escript calls main/1 with the list of arguments. Pattern matching on the list is the whole parser: one clause for add, one for list, and one catch-all for the usage line:
todo/lib/todo/cli.ex
defmodule Todo.CLI do @moduledoc false def main(["add", item]) do File.write!("todo.txt", item <> "\n", [:append]) IO.puts("added: #{item}") end def main(["list"]) do case File.read("todo.txt") do {:ok, body} -> body |> String.split("\n", trim: true) |> Enum.each(fn item -> IO.puts("- " <> item) end) {:error, :enoent} -> IO.puts("the list is empty") end end def main(_args) do IO.puts("usage: todo add ITEM | todo list") end end
Build and run
mix escript.build compiles the project and writes the executable named after the application. The commands after it run the binary, not Mix:
cd todo
sed -i 's/ app: :todo,/ app: :todo,\n escript: [main_module: Todo.CLI],/' mix.exs
mix escript.build
Compiling 2 files (.ex)
Generated todo app
Generated escript todo with MIX_ENV=dev
./todo add "buy milk"
added: buy milk
./todo list
- buy milk
./todo
usage: todo add ITEM | todo list
The three runs show the three branches of main/1. The executable is one file of about a megabyte, because it carries the runtime; it can be copied to a machine of the same architecture and run there.
The file store
The store is one text file, one item per line. File.write!/3 with [:append] adds without reading the old content, and File.read/1 returns {:ok, body} or {:error, :enoent}, which the case matches. The files lesson shows the rest of the API, and a real program places the file in a data folder from runtime.exs rather than the current folder.
The project and the program were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which key of
mix.exsnames the entry module of an escript?Answer
escript: [main_module: Todo.CLI]
The escript key names the module whose main/1 the binary calls.
-
What does an escript call in its entry module?
Answer
main/1 with the list of arguments
The escript convention is main/1, and the arguments arrive as a list of strings.
-
What does
mix escript.buildwrite?Answer
An executable named after the application
The task builds the escript file in the project root.
-
Why does the executable run without Elixir installed?
Answer
It carries the compiled beams and the runtime
The escript is self-contained for the same architecture.
-
What does this program print?
defmodule T do def main(["add", item]), do: IO.puts("added: " <> item) def main(_), do: IO.puts("usage") end T.main(["add", "milk"])Answer
added: milk
The first clause matches the two-element list and prints the item.
-
What does this program print?
defmodule T do def main(["add", item]), do: IO.puts("added: " <> item) def main(_), do: IO.puts("usage") end T.main([])Answer
usage
The empty list matches the catch-all clause.
-
What does
File.write!("todo.txt", item <> "\n", [:append])do?Answer
Adds the line to the end of the file
The append option keeps the old content and adds the new line.
-
What does
File.read/1answer for a file that does not exist?Answer
{:error, :enoent}
The read returns an error tuple with the reason :enoent.
-
Which file does the
addcommand write to?Answer
todo.txt in the current folder
The program writes todo.txt next to where it runs.
-
What is the difference between an escript and a release?
Answer
An escript is one executable; a release is a folder with the runtime and the application
The escript packs one file; the release ships a folder with bin scripts and ERTS.
Article author: Arthur Isaev