View Source Prompt.Router behaviour (prompt v0.10.1)

Router for Prompt

Simplifies defining commands, sub-commands and arguments.

Choose the module responsible for taking the command line arguments and use Prompt.Router, otp_app: :your_app at the top.

Then simply define your commands and arguments.

Exposes a main/1 function that is called with the command line args

Arguments

See arg/3

Example

defmodule My.CLI do
  use Prompt.Router, otp_app: :my_app

  command :checkout, My.CheckoutCommand do
    arg :help, :boolean
    arg :branch, :string, short: :b, default: "main"
  end

  command "", My.DefaultCommand do
    arg :info, :boolean
  end
end

defmodule My.CheckoutCommand do
  use Prompt.Command

  @impl true
  def process(arguments) do
    # arguments will be a map of the defined arguments and their values
    # from the command line input
    # If someone used the command and passed `--branch  feature/test`, then
    # `argmuments would look like `%{help: false, branch: "feature/test"}`
    display("checking out " <> arguments.branch)
  end
end

defmodule My.DefaultCommand do
  use Prompt.Command

  @impl true
  def init(arguments) do
    # you can implement the `c:init/1` callback to transform
    # the arguments before `c:process/1` is called if you want
    arguments
  end
  
  @impl true
  def process(arguments) do
    # arguments will have a key of `:leftover` for anything
    # passed to the command that doesn't have a `arg` defined.
    # IF someone called this with `--info --test something`, then then
    # arguments will look like `%{info: true, leftover: ["--test", "something"]}`
    display("showing info")
  end
end

Summary

Callbacks

Called when the flag --completions string is passed on the command-line.

This function is called after the main function is done.

Prints help to the screen when there is an error, or --help is passed as an argument.

Prints help to the screen when there is an error with a string indicating the error

The function responsible for filtering and calling the correct command module based on command line input

Prints the version from the projects mix.exs file

Functions

Defines the arguments of a command.

Name of the subcommand that is expectedaany()

Callbacks

generate_completions(binary)

@callback generate_completions(binary()) :: non_neg_integer()

Called when the flag --completions string is passed on the command-line.

The default behaviour for this is to write the completion script to the screen.

zsh is supported out of the box. If other completion scripts are required, this callback will need to be implemented.

see https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org

Overrideable

handle_exit_value(any)

@callback handle_exit_value(any()) :: no_return()

This function is called after the main function is done.

It does it's best to handle any value returned from a command and turn it into an integer, 0 being a successful command and any non-zero being an error.

Overrideable

help()

@callback help() :: non_neg_integer()

Prints help to the screen when there is an error, or --help is passed as an argument.

Overridable

help(t)

@callback help(String.t()) :: non_neg_integer()

Prints help to the screen when there is an error with a string indicating the error

Overridable

main(list)

@callback main([binary()]) :: no_return()

The function responsible for filtering and calling the correct command module based on command line input

version()

@callback version() :: non_neg_integer()

Prints the version from the projects mix.exs file

Overridable

Functions

arg(arg_name, arg_type, opts \\ [])

(macro)

Defines the arguments of a command.

Argument Name

This indicates what the user will type as the option to the sub-command. For example,

arg :print, :boolean

would allow the user to type $ your_command --print

Options

Available options are:

  • default - a default value if the user doesn't use this option
  • short - an optional short argument option i.e short: :h would all the user to type -h

command(name, module)

(macro)

command(name, module, list)

(macro)

Name of the subcommand that is expectedaany()

Takes an atom or string as the command name and a Prompt.Command module.