← Back to lesson 26: Turn a project into a command

Turn a project into a command: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Add a custom Mix task and configure an escript entry point.

Build an escript and run its main/1 function from the system terminal.

Run this first

Run it once, change one input, and compare the new result.

Original full example
# Run after configuring main_module in mix.exs
mix escript.build
./score_card 60 92 41

The executable receives three text arguments through main/1.

Steps for the full example

  1. Read escript: [main_module: App.CLI]: Choose the module that owns main/1.
  2. Read mix escript.build: Build the executable file.
  3. Read System.argv/0: Read command-line arguments inside a running VM when needed.

Original lesson notes

Run the example first. Predict one result, then change one input and run it again.

A custom Mix task automates a project command under a namespaced task module.

An escript packages BEAM bytecode behind one executable command for systems with Erlang installed.

Symbols and terms
escript: [main_module: App.CLI]
Choose the module that owns main/1.
mix escript.build
Build the executable file.
System.argv/0
Read command-line arguments inside a running VM when needed.
Mix task
A custom Mix task automates a project command under a namespaced task module.
escript
An escript packages BEAM bytecode behind one executable command for systems with Erlang installed.
entry point
The configured main/1 function receives command-line arguments.

Original full exercise

Write a main/1 head that prints every argument on its own line.

Exercise starter
def main(args) do
  Enum.each(args, fn arg -> ____(arg) end)
end

Each argument should appear once.

Show hint

Use IO.puts/1.

Show original answer
Reference answer
def main(args) do
  Enum.each(args, fn arg -> IO.puts(arg) end)
end
Does escript remove the need for Erlang?

No. The target system still needs an Erlang runtime.

Original lesson takeaways
Design trade-offs and language comparisons
Original course references