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.
# Run after configuring main_module in mix.exs
mix escript.build
./score_card 60 92 41The executable receives three text arguments through main/1.
Steps for the full example
- Read
escript: [main_module: App.CLI]: Choose the module that ownsmain/1. - Read
mix escript.build: Build the executable file. - 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/1function receives command-line arguments.
Original full exercise
Write a main/1 head that prints every argument on its own line.
def main(args) do
Enum.each(args, fn arg -> ____(arg) end)
endEach argument should appear once.
Show hint
Use IO.puts/1.
Show original answer
def main(args) do
Enum.each(args, fn arg -> IO.puts(arg) end)
endDoes escript remove the need for Erlang?
No. The target system still needs an Erlang runtime.
Original lesson takeaways
- Automate repeated project work with Mix tasks.
- Use
main/1as a small CLI boundary. - Keep parsing and business rules outside the entry-point module.