ExPrompt (ex_prompt v0.2.0) View Source

ExPrompt is a helper package to add interactivity to your command line applications as easy as possible.

It allows common operations such as:

  • Asking for an answer.
  • Asking for a "required" answer.
  • Choosing between several options.
  • Asking for confirmation.
  • Asking for a password.

Link to this section Summary

Functions

Asks the user to select form a list of choices. It returns either the index of the element in the list or -1 if it's not found.

Asks for confirmation to the user. It allows the user to answer or respond with the following options

Asks for a password.

Reads a line from :stdio displaying the prompt that is passed in.

Same as string/2 but it will continue to "prompt" the user in case of an empty response.

Link to this section Types

Specs

choices() :: [String.t()]

Specs

prompt() :: String.t()

Link to this section Functions

Link to this function

choose(prompt, choices, default \\ -1)

View Source

Specs

choose(prompt(), choices(), integer()) :: integer()

Asks the user to select form a list of choices. It returns either the index of the element in the list or -1 if it's not found.

This method tries first to get said element by the list number, if it fails it will attempt to get the index from the list of choices by the value that the user wrote.

Examples

To ask for a favorite color in a predefined list

ExPrompt.choose("Favorite color?" , ~w(red green blue))

It's the same example above, but defines to the second option (green) as default value if none is selected.

ExPrompt.choose("Favorite color?" , ~w(red green blue), 2)

Link to this function

confirm(prompt, default \\ nil)

View Source

Specs

confirm(prompt(), boolean() | nil) :: boolean()

Asks for confirmation to the user. It allows the user to answer or respond with the following options:

  • Yes, yes, YES, Y, y
  • No, no, NO, N, n

In case that the answer is none of the above, it will prompt again until we do or the default value if it's present.

Examples

To ask whether the user wants to delete a file or not:

ExPrompt.confirm("Are you sure you want to delete this file?")

It's the same example above, returning false as default.

ExPrompt.confirm("Are you sure you want to delete this file?", false)

Link to this function

get(prompt, default \\ "")

View Source

Specs

get(prompt(), String.t()) :: String.t()

Alias for string/2.

Specs

get_required(prompt()) :: String.t()

Alias for string_required/1.

Link to this function

password(prompt, hide \\ true)

View Source

Specs

password(prompt(), hide :: boolean()) :: String.t()

Asks for a password.

This method will hide the password by default as the user types. If that's not the desired behavior, it accepts false, and the password will be shown as it's being typed.

Examples

ExPrompt.password("Password: ")

ExPrompt.password("Password: ", false)

Link to this function

string(prompt, default \\ "")

View Source

Specs

string(prompt(), String.t()) :: String.t()

Reads a line from :stdio displaying the prompt that is passed in.

In case of any errors or :eof this function will return the default value if present, or an empty string otherwise.

Examples

To ask a user for their name and await for the input:

ExPrompt.string("What is your name? ")

To ask for a hostname defaulting to localhost:

ExPrompt.string("What is the hostname? ", "localhost")

Specs

string_required(prompt()) :: String.t()

Same as string/2 but it will continue to "prompt" the user in case of an empty response.

Link to this function

yes?(prompt, default \\ nil)

View Source

Specs

yes?(prompt(), boolean() | nil) :: boolean()

Alias for confirm/2.