Struct inquire::Confirm[][src]

pub struct Confirm<'a> {
    pub message: &'a str,
    pub default: Option<bool>,
    pub placeholder: Option<&'a str>,
    pub help_message: Option<&'a str>,
    pub formatter: BoolFormatter<'a>,
    pub parser: BoolParser<'a>,
    pub default_value_formatter: BoolFormatter<'a>,
    pub error_message: String,
    pub render_config: RenderConfig,
}
Expand description

Prompt to ask the user for simple yes/no questions, commonly known by asking the user displaying the (y/n) text.

This prompt is basically a wrapper around the behavior of CustomType prompts, providing a sensible set of defaults to ask for simple true/false questions, such as confirming an action.

Default values are formatted with the given value in uppercase, e.g. (Y/n) or (y/N). The bool parser accepts by default only the following inputs (case-insensitive): y, n, yes and no. If the user input does not match any of them, the following error message is displayed by default:

  • # Invalid answer, try typing 'y' for yes or 'n' for no.

Finally, once the answer is submitted, Confirm prompts display the bool value formatted as either “Yes”, if a true value was parsed, or “No” otherwise.

The Confirm prompt does not support custom validators because of the nature of the prompt. The user input is always parsed to true or false. If one of the two alternatives is invalid, a Confirm prompt that only allows yes or no answers does not make a lot of sense to me, but if someone provides a clear use-case I will reconsider.

Confirm prompts provide several options of configuration:

  • Prompt message: Required when creating the prompt.
  • Default value: Default value returned when the user submits an empty response.
  • Placeholder: Short hint that describes the expected value of the input.
  • Help message: Message displayed at the line below the prompt.
  • Formatter: Custom formatter in case you need to pre-process the user input before showing it as the final answer.
    • Formats true to “Yes” and false to “No”, by default.
  • Parser: Custom parser for user inputs.
    • The default bool parser returns true if the input is either "y" or "yes", in a case-insensitive comparison. Similarly, the parser returns false if the input is either "n" or "no".
  • Default value formatter: Function that formats how the default value is displayed to the user.
    • By default, displays “y/n” with the default value capitalized, e.g. “y/N”.
  • Error message: Error message to display when a value could not be parsed from the input.
    • Set to “Invalid answer, try typing ‘y’ for yes or ‘n’ for no” by default.

Example

use inquire::Confirm;

let ans = Confirm::new("Do you live in Brazil?")
    .with_default(false)
    .with_help_message("This data is stored for good reasons")
    .prompt();

match ans {
    Ok(true) => println!("That's awesome!"),
    Ok(false) => println!("That's too bad, I've heard great things about it."),
    Err(_) => println!("Error with questionnaire, try again later"),
}

Fields

message: &'a str

Message to be presented to the user.

default: Option<bool>

Default value, returned when the user input is empty.

placeholder: Option<&'a str>

Short hint that describes the expected value of the input.

help_message: Option<&'a str>

Help message to be presented to the user.

formatter: BoolFormatter<'a>

Function that formats the user input and presents it to the user as the final rendering of the prompt.

parser: BoolParser<'a>

Function that parses the user input and returns the result value.

default_value_formatter: BoolFormatter<'a>

Function that formats the default value to be presented to the user

error_message: String

Error message displayed when a value could not be parsed from input.

render_config: RenderConfig

RenderConfig to apply to the rendered interface.

Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.

When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still suport NO_COLOR, you will have to do this on your end.

Implementations

Default formatter, set to DEFAULT_BOOL_FORMATTER

Default input parser.

Default formatter for default values, mapping true to [“Y/n”] and false to [“y/N”]

Default error message displayed when parsing fails.

Creates a Confirm with the provided message and default configuration values.

Sets the default input.

Sets the placeholder.

Sets the help message of the prompt.

Sets the formatter.

Sets the parser.

Sets a custom error message displayed when a submission could not be parsed to a value.

Sets the default value formatter

Sets the provided color theme to this prompt.

Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.

When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still suport NO_COLOR, you will have to do this on your end.

Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.

This method is intended for flows where the user skipping/cancelling the prompt - by pressing ESC - is considered normal behavior. In this case, it does not return Err(InquireError::OperationCanceled), but Ok(None).

Meanwhile, if the user does submit an answer, the method wraps the return type with Some.

Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Performs the conversion.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.