Elixir basics · Lesson 25 (open contents)
Turn a project into a command
Add a custom Mix task and configure an escript entry point.
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 41- The executable receives three text arguments through
main/1.
Read from the first line down
- 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.
Symbols are not secret signs
escript: [main_module: App.CLI]Choose the module that owns main/1.
mix escript.buildBuild the executable file.
System.argv/0Read command-line arguments inside a running VM when needed.
Match each name to its meaning
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.
Why these forms are useful
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.
Close the answer and try
Write a main/1 head that prints every argument on its own line.
def main(args) do
Enum.each(args, fn arg -> ____(arg) end)
endTarget result: Each argument should appear once.
Stuck? Read one hint
Use IO.puts/1.
After you run it, see one answer
def main(args) do
Enum.each(args, fn arg -> IO.puts(arg) end)
endThink about it: Does escript remove the need for Erlang?
No. The target system still needs an Erlang runtime.
Remember these three lines
- 1
Automate repeated project work with Mix tasks.
- 2
Use
main/1as a small CLI boundary. - 3
Keep parsing and business rules outside the entry-point module.