pub fn ask(
question: impl AsRef<str>,
stdin: &mut impl Read,
stdout: &mut impl Write
) -> Result<Answer, Error>Expand description
Ask the user a yes or no question on stdout, reading the reply from stdin.
Replies are delimited by newlines of any kind and must be one of ‘’ (maps to yes), ‘y’, ‘yes’, ‘n’, ‘no’, case insensitive. If the reply fails to parse, the question will be asked again ad infinitum.
Examples
use ask_cli::{ask, Answer};
assert!(matches!(
ask("Continue? [Y/n] ", &mut "y\n".as_bytes(), &mut io::sink()),
Ok(Answer::Yes)
));
assert!(matches!(
ask("Continue? [Y/n] ", &mut "n\n".as_bytes(), &mut io::sink()),
Ok(Answer::No)
));
assert!(matches!(
ask("Continue? [Y/n] ", &mut "".as_bytes(), &mut io::sink()),
Ok(Answer::Unknown)
));
// Here we use 3 different kinds of line endings
let mut stdout = Vec::new();
ask(
"Continue? [Y/n] ",
&mut "a\nb\rc\r\nyes\n".as_bytes(),
&mut stdout,
)
.unwrap();
assert_eq!(
"Continue? [Y/n] Continue? [Y/n] Continue? [Y/n] Continue? [Y/n] ",
from_utf8(&stdout).unwrap()
);Errors
Underlying I/O errors are bubbled up.