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: &'a 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” andfalse
to “No”, by default.
- Formats
- Parser: Custom parser for user inputs.
- The default
bool
parser returnstrue
if the input is either"y"
or"yes"
, in a case-insensitive comparison. Similarly, the parser returnsfalse
if the input is either"n"
or"no"
.
- The default
- 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: &'a RenderConfig
RenderConfig to apply to the rendered interface.
Implementations
Default formatter, set to DEFAULT_BOOL_FORMATTER
Default input parser.
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.
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
Trait Implementations
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Confirm<'a>
impl<'a> !UnwindSafe for Confirm<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more