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” 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: 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
sourceimpl<'a> Confirm<'a>
impl<'a> Confirm<'a>
sourcepub const DEFAULT_FORMATTER: BoolFormatter<'a>
pub const DEFAULT_FORMATTER: BoolFormatter<'a>
Default formatter, set to DEFAULT_BOOL_FORMATTER
sourcepub const DEFAULT_PARSER: BoolParser<'a>
pub const DEFAULT_PARSER: BoolParser<'a>
Default input parser.
sourcepub const DEFAULT_DEFAULT_VALUE_FORMATTER: BoolFormatter<'a>
pub const DEFAULT_DEFAULT_VALUE_FORMATTER: BoolFormatter<'a>
sourcepub const DEFAULT_ERROR_MESSAGE: &'a str
pub const DEFAULT_ERROR_MESSAGE: &'a str
Default error message displayed when parsing fails.
sourcepub fn new(message: &'a str) -> Self
pub fn new(message: &'a str) -> Self
Creates a Confirm with the provided message and default configuration values.
sourcepub fn with_default(self, default: bool) -> Self
pub fn with_default(self, default: bool) -> Self
Sets the default input.
sourcepub fn with_placeholder(self, placeholder: &'a str) -> Self
pub fn with_placeholder(self, placeholder: &'a str) -> Self
Sets the placeholder.
sourcepub fn with_help_message(self, message: &'a str) -> Self
pub fn with_help_message(self, message: &'a str) -> Self
Sets the help message of the prompt.
sourcepub fn with_formatter(self, formatter: BoolFormatter<'a>) -> Self
pub fn with_formatter(self, formatter: BoolFormatter<'a>) -> Self
Sets the formatter.
sourcepub fn with_parser(self, parser: BoolParser<'a>) -> Self
pub fn with_parser(self, parser: BoolParser<'a>) -> Self
Sets the parser.
sourcepub fn with_error_message(self, error_message: &'a str) -> Self
pub fn with_error_message(self, error_message: &'a str) -> Self
Sets a custom error message displayed when a submission could not be parsed to a value.
sourcepub fn with_default_value_formatter(self, formatter: BoolFormatter<'a>) -> Self
pub fn with_default_value_formatter(self, formatter: BoolFormatter<'a>) -> Self
Sets the default value formatter
sourcepub fn with_render_config(self, render_config: RenderConfig) -> Self
pub fn with_render_config(self, render_config: RenderConfig) -> Self
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.
sourcepub fn prompt_skippable(self) -> InquireResult<Option<bool>>
pub fn prompt_skippable(self) -> InquireResult<Option<bool>>
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
.
sourcepub fn prompt(self) -> InquireResult<bool>
pub fn prompt(self) -> InquireResult<bool>
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> !Send for Confirm<'a>
impl<'a> !Sync for Confirm<'a>
impl<'a> Unpin for Confirm<'a>
impl<'a> !UnwindSafe for Confirm<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more