View Source Prompt.Command behaviour (prompt v0.10.0)

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 process/1 which handles all of the side effects of the command itself.

example

Example

defmodule MyCommand do
  @moduledoc "MyCommand's help message help() is defined in the __using__ macro that prints this message if called"

  use Prompt.Command

  @impl true
  def init(_argv) do
    # parse list of args to a struct if desired
    %SomeStruct{list: true, help: false, directory: "path/to/dir"}
  end

  @impl true
  def process(%{help: true}), do: help() # this help function is defined by default in the macro that prints the @moduledoc when called
  def process(%{list: true, help: false, directory: dir}) do
    display(File.ls!(dir))
  end

end

If this is used in a release, help() won't print the @moduledoc correctly because releases strip documentation by default. For this to work correctly, tell the release to keep docs:

releases: [
  appname: [
    strip_beams: [keep: ["Docs"]]
  ]
]

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

@callback help() :: :ok

Prints the help available for this command

@callback init(map()) :: term()

Takes the options passed in via the command line and tramforms them into a struct that the process command can handle

@callback process(term()) :: :ok | {:error, binary()}

Processes the command and does the things required