Creating command line programs in Elixir

Published on Author Akhil BansalLeave a comment

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:

defmodule VowelCounter.Mixfile do
  use Mix.Project

  def project do
    [app: :vowel_counter,
     version: "0.0.1",
     elixir: "~> 1.1",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    [applications: [:logger]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    []
  end
end

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:

defmodule VowelCounter.Mixfile do
  use Mix.Project

  def project do
    [app: :vowel_counter,
     version: "0.0.1",
     elixir: "~> 1.1",
     escript: escript_config, 
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    [applications: [:logger]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    []
  end

  defp escript_config do
    [main_module: VowelCounter]
  end
end

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:

** (UndefinedFunctionError) undefined function: VowelCounter.main/1
    (vowel_counter) VowelCounter.main(["my test string"])
    (elixir) lib/kernel/cli.ex:76: anonymous fn/3 in Kernel.CLI.exec_fun/2

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

defmodule VowelCounter do
  # Take command line arguments
  # Then parse them
  # Then count number of vowel
  # Print the outpu
  def main(args) do
    args 
      |> parse_args
      |> count_vowel
      |> IO.puts
  end

  defp parse_args(args) do
    # Covering only expected arguments. We can add code to handle cases when unexpected arguments are passed or help text etc.
    # Checkout OptionParser lib for details. 
    {_, [str], _} = OptionParser.parse(args)
    str
  end

  defp count_vowel(str) do
    # Scan for vowels and take the list's length
    len = length(Regex.scan(~r/[aeiou]/i, str))
    "There are #{len} vowels"
  end
end

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.

Leave a Reply

Your email address will not be published. Required fields are marked *