Writing command line programs in Elixir: Vowel Counter

Note: This is a very basic example of mix tool and escript utility. One can skip this post if already have an idea about these tools.

Lets write a simple command line program which would take a String as a command line argument and display the number of vowels in it. We'll use Elixir's mix build tool and OptionParser to parse command line arguments.

Lets start by setting up the application:

$ mix new vowel_counter

Open mix.exs, you will see something like:

We'll be using Erlang's escript utility to precompile our app and package our application's code along with its dependencies(no dependencies in this example app) in one single executable. So lets add escript: escript_config to project method in our mix.exs to define our escript configuration, also define a private method escript_config to specify the the main_module. In this case, after these changes our mix.exs would look like:

Lets compile and run our application by running:
$ mix escript.build
$ ./vowel_counter "my test string"

Since we have specified VovelCounter module as our main_module in elixir_config, our app expects and call the main method from this module and pass command line arguments to it. We haven't defined this main method yet, so we should see something similar when we execute our app:

Lets open lib/vowel_counter.ex and define main method as:

Lets re-compile and re-run our application by:
$ mix escript.build
$ ./vowel_counter "This string has 5 vowels."
We should see: "There are 5 vowels"

Your comments/suggestions are most welcome.