Step 3: Make a Command-Line Executable

Understand the details of executing the command line.

We'll cover the following

Although we can run our code by calling the run function via mix, it isn’t friendly for other users. So, let’s create something we can run from the command line.

Update mix.exs file

Mix can package our code, along with its dependencies, into a single file that can be run on any Unix-based platform. This uses Erlang’s escript utility, which can run precompiled programs stored as a ZIP archive. In our case, the program will be run as issues.

When escript runs a program, it looks in the mix.exs file for the option escript. This should return a keyword list of escript configuration settings. The most important of these is main_module:, which must be set to the name of a module containing a main function. It passes the command-line arguments to this main function as a list of character lists (not binaries). As this seems to be a command-line concern, we’ll put the main function in Issues.CLI. Here’s the update to mix.exs:

defmodule Issues.MixProject do
  use Mix.Project

  def project do
    [
      app:             :issues,
      escript:         escript_config(),
      version:         "0.1.0",
      elixir:          "~> 1.6-dev",
      start_permanent: Mix.env() == :prod,
      deps:            deps()
    ]
  end

  def application do
    [
      extra_applications: [:logger]
    ]
  end

  defp deps do
    [
      { :httpoison, "~> 1.0.0" },
      { :poison,    "~> 3.1"    },
    ]
  end
  defp escript_config do
    [
      main_module: Issues.CLI
    ]
  end
end

Get hands-on with 1200+ tech skills courses.