Skip to content

Latest commit

 

History

History
84 lines (60 loc) · 1.9 KB

example.livemd

File metadata and controls

84 lines (60 loc) · 1.9 KB

Simple Example

Mix.install([
  {:prompt, "~> 0.9.3"}
])

Starting Out

Lets go through an example of creating and building a new commandline/terminal app with Prompt that will acknowledge a person. Lets call it ack.

Lets define a CLI module and create where we can define what arguments we allow and what options are available for our tool.

defmodule CLI do
  @moduledoc """
  DOCUMENTATION FOR THE TOOL
  """
  use Prompt.Router, otp_app: :ack

  command :hello, HelloCommand do
    arg(:help, :boolean)
  end
end

Now Create the Command Handler

Now we'll have to create the handler for the hello command.

defmodule HelloCommand do
  @moduledoc """
  The help message

  ack hello <name>

    --help prints this help message

  """
  use Prompt.Command

  @impl true
  def init(opts) do
    # opts is map of data which includes the 
    # options for the command and any other data passed in
    # Our opts here look like give the following incanatation `ack hello bob`:
    # `%{help: false, leftover: ["bob"]}

    # `init/1` can be used to transform any arguments if needed.
    Map.put(opts, :name, List.first(opts.leftover, nil))
  end

  @impl true
  def process(%{help: true}), do: help()

  def process(%{name: name}) do
    display("Hello #{name}", color: :green)
  end
end

Simulate calling the CLI

CLI.main(["hello", "bob"])
CLI.main(["hello", "--help"])

Bundle and Distribute

To build your app, you'll need to decide if you want an escript or a binary. The former will require the user of your tool to have Elixir and Erlang installed on their system. A binary built using Bakeware or Burrito can be distributed and used without having Elixir or Erlang on the system.

See the documentation for details on both