Use OptionParser.parse
to parse user-supplied command line arguments into a keyword list:
The return value is a triple (a 3-size tuple) containing three lists:
- a keyword list of flags/switches and their values
- a list of other non-switch arguments
- a keyword list of invalid flags and their values
You can pattern match the resulting tuple:
Supply the strict
parameter for enforcing allowed flags:
Invalid flags such as verbose
in the above example are returned in the third list of the OptionParser.parse
result.
Elixir converts flags/switches to underscore atoms, so –source-path becomes :source_path, to better suit Elixir conventions. This means that option names on the command line cannot contain underscores; such options will be put in the invalid options list.
strict
can also enforce argument types:
The above limit
flag failed because we expected integer
, but received string
instead.
Additional reading:
- OptionParser
iex> h OptionParser.parse