Prompt.Command behaviour (prompt v0.5.7) View Source
Helps users define and build command line commands.
Defines the behaviour for a Command.
We expect init/1
to be called with the command line options and get
back a data structure that is passed to parse/1
which handles all
of the side effects of the command itself.
Example
defmodule MyCommand do
@moduledoc "MyCommand's help message"
use Prompt.Command
@impl true
def init(args) do
# parse list of args to map or struct
%{list: true, help: false, directory: "path/to/dir"}
end
@impl true
def process(%{help: true}), do: help()
def process(%{list: true, help: false, directory: dir}) do
display(File.ls!(dir))
end
@impl true
def help(), do: display(@moduledoc)
end
Typically one will use the OptionParser.parse/1
function to parse
the command
defp parse(argv) do
argv
|> OptionParser.parse(
strict: [help: :boolean, directory: :string, list: :boolean],
aliases: [h: :help, d: :directory]
)
|> _parse()
end
defp _parse({opts, _, _}) do
help = Keyword.get(opts, :help, false)
dir = Keyword.get(opts, :directory, "./")
list = Keyword.get(opts, :length, true)
%{help: help, directory: dir, list: list}
end
Link to this section Summary
Callbacks
Prints the help available for this command
Takes the options passed in via the command line and tramforms them into a struct that the process command can handle
Processes the command and does the things required
Link to this section Callbacks
Specs
help() :: :ok
Prints the help available for this command
Specs
Takes the options passed in via the command line and tramforms them into a struct that the process command can handle
Specs
Processes the command and does the things required