Last Updated: February 25, 2016
·
2.367K
· bbense

Elixir Command Line Parsing

I haven't had much luck finding a complete example of command line parsing in Elixir, so I thought I'd share what I've come up with so far.

This example shows uses a Map to ultimately store the options as key, value pairs. This implies that there can be only one value for any given option.

def main(args) do
    args |> parse_args |> process
end

This function defines the main flow of control in the program.

def parse_args(args) do
  options = %{ :count => @max_ofiles ,
           :type  => @default_type
           }
cmd_opts = OptionParser.parse(args, 
    switches: [help: :boolean , count: :integer],
    aliases: [h: :help, c: :count])

    case cmd_opts do
      { [ help: true], _, _}   -> :help
      { [], args, [] }         -> { options, args }
      { opts, args, [] }       -> { Enum.into(opts,options), args }
      { opts, args, bad_opts}  -> { 
                             Enum.into(merge_opts(opts,bad_opts),
                                options), 
                                args
                               }
        _                        -> :help
    end
  end

The main command line argument parsing function. It sets up an option map with default values and then merges in any values from the command line. It also allows undefined options, see rehabilitate_args below.

def merge_opts(opts,bad_opts) do
  bad_opts |>  rehabilitate_args |> Keyword.merge(opts)
end 

A simple helper function to make the main parse routine less complicated.

def rehabilitate_args(bad_args) do
  bad_args 
 |> 
  Enum.flat_map(fn(x) -> Tuple.to_list(x) end)
 |> 
   Enum.filter_map(fn(str) -> str end, 
                 fn(str) -> 
                   String.replace(str, ~r/^\-([^-]+)/, "--\\1") 
                 end )
 |>
   OptionParser.parse
 |> 
   Tuple.to_list
 |>
   List.first
 end

The function rehabilitate_args is something I've included since my application will use plugins and I would like plugin authors to be able to simply use command line args w/o a complicated interface. This might or might not be a good idea and is mostly included as an example of how to handle bad_args if you want to. If you use :strict rather than :switches in the OptionParser.parse call undefined options will automatically generate an error.

def process(:help) do 
  IO.puts @module_doc
  System.halt(0)
end

def process({options,args}) do
   AllTheRealWork.start
end

This is an example of using elixir's pattern matching in function definitions to allow it to route flow of control in the program. The value returned from the parse_args call will determine which version of the process function gets run.