Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# Simple Example ```elixir 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`. <!-- livebook:{"break_markdown":true} --> Lets define a `CLI` module and create where we can define what arguments we allow and what options are available for our tool. ```elixir 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. ```elixir 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 ```elixir CLI.main(["hello", "bob"]) ``` ```elixir 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](https://hexdocs.pm/prompt/Prompt.html#module-building-for-distribution) for details on both
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run on your machine

with Livebook Desktop

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More ×