← All series

Series contents

Log in to track your progress

  1. 1. Welcome to Alchemist Camp Episode
  2. 2. Lesson 1: The guessing game Episode
  3. 3. Solution 1: The Greeter Episode
  4. 4. Lesson 2: Word Count Episode
  5. 5. Word Count ULTIMATE EDITION! Episode
  6. 6. Minimal Todo List Part 1 Episode
  7. 7. Elixir lists vs Elixir tuples Article
  8. 8. The Image Snatcher Episode
  9. 9. Minimal Todo List Part 2 Episode
  10. 10. Elixir maps made effortless Article
  11. 11. Fibonacci with tail recursion Episode
  12. 12. Mini Markdown Episode
  13. 13. Extending Mini Markdown Episode
  14. 14. Alchemy Markdown Episode
  15. 15. Using mix to modularize and add tests Episode
  16. 16. Command line applications with escript Episode
  17. 17. Caboose, a simple Unix tail clone Episode
  18. 18. Game board (Tictac Part 1) Episode
  19. 19. Structs: Maps with checks and default values Article
  20. 20. Deck generation, Pythagorean Triplets and more Episode
  21. 21. Whiteboarding the Tictac state machine Episode
  22. 22. Game state machine (Tictac Part 2) Episode
  23. 23. Modelling the memory game Episode
  24. 24. Finishing Tictac and making a CLI Episode
  25. 25. Threatened by rooks, bishops and knights Episode
  26. 26. A worker to log stats from a YouTube’s API Episode
  27. 27. Constructing bitmap files with Elixir Episode
  28. 28. Making bitmaps with color palettes Episode
  29. 29. Flattening lists recursively in Elixir Episode

Command line applications with escript

We use our new-found powers to create a command line application that can be run directly from the shell and can handle arguments. Since we've already made a word count utility way back in the Word Count lesson, we'll use it as a starting point.

Elixir has a feature called escript, which can be used to make more robust command line applications. The first thing we need to do is open up our mix file and add an entry point for our script. Inside the project block, add:

escript: [main_module: WordCount.CLI],

Then we'll make a WordCount.CLI file inside our /lib directory.

Escript entry points

Inside our new file we have to implement a main function that takes args and does something based on the input. We'll use OptionParser to parse the arguments passed in from the command line. It's a convenient little utility that extracts args, switches (which we did manually in lesson 2), and aliases (which we didn't support at all).

To keep our CLI handling separate from our main app logic, we'll just call our main WordCount module with the parsed arguments and flags extracted by OptionParser.parse.

The main module

Much of the main word count module will be familiar from Word Count ULTIMATE EDITION. The structure goes as follows:

  1. If the input is valid, open the specified file
  2. Read in the entire file as one binary (this could be changed to Stream functions if we needed to support very large inputs)
  3. Split the file into lines
  4. Split the file into words using Regex and Enum.filter
  5. Split the file into characters using Regex and Enum.filter
  6. For each flag enabled, report the appropriate count

And then we're done!

Note: The word "native" was ill-chosen. Escript will let you run elixir programs directly from the command line, but they still run through the Erlang VM as opposed to being compiled for an x86 processor as with C or Rust.

Challenge 7

Use escript implement a simple clone of Unix's tail command. Implement its --lines flag and its -n alias.

(solution here)

Open standalone page ↗

No Comments