pub trait Promptable: Sized {
    fn prompt<S: AsRef<str>>(msg: S) -> Result<Self, ReadlineError>;
    fn prompt_opt<S: AsRef<str>>(msg: S) -> Result<Option<Self>, ReadlineError>;
    fn prompt_default<S: AsRef<str>>(
        msg: S,
        default: Self
    ) -> Result<Self, ReadlineError>; }
Expand description

A trait for convenient, opinionated prompting

Required Methods

Prompts for a value. Re-prompts on invalid and empty input.

Prompts for a value, returning None for empty input. Re-prompts on invalid input.

Prompts for a value with a default value for empty input. Re-prompts on invalid input.

The default value will be mentioned in the prompt message

Implementations on Foreign Types

Prompt until you get a non-empty string

use promptly::Promptable;
String::prompt("Enter your name")?;

Prompt for an optional string

use promptly::Promptable;
String::prompt_opt("Enter your phone number (optional)")?;

Prompt for a string with a provided fallback value if empty.

use promptly::Promptable;
String::prompt_default("Enter your country", "USA".into())?;

Default value is visible in the prompt as: (default=USA)

PathBuf prompting will use a path autocompleter

Prompt until you get a non-empty path

Prompt for an optional path

Prompt for a path with a provided fallback value if empty

Specialized bool prompter that supports yes/no (y/n) values

Prompt for bool represented as true/false, yes/no, or y/n input

The prompt will display the options: (y/n)

use promptly::Promptable;
bool::prompt("Do you accept the terms?")?;

Prompt for optional bool input. Empty input returns None.

The prompt will display the options: (y/n)

use promptly::Promptable;
bool::prompt_opt("Did you even read this question?")?;

Prompt for optional bool input. Empty input returns None.

The prompt will also display the options: (Y/n) or (y/N) depending on the default

use promptly::Promptable;
bool::prompt_default("Would you like to send us money?", true)?;

Implementors