Struct Prompt

Source
pub struct Prompt<'a> { /* private fields */ }
Expand description

Untyped builder for reading and parsing CLI input.

Implementations§

Source§

impl<'a> Prompt<'a>

Source

pub fn new(message: &'a str) -> Self

Create a new prompt with a message.

Source

pub fn default(self, default: &str) -> Self

Provide a default value as string. If the user hits ENTER with empty input, default will be used and parsed as the target type.

Examples found in repository?
examples/basic.rs (line 5)
3fn main() -> Result<(), askit::Error> {
4    let name: String = prompt("Name: ").get()?;
5    let age: u8 = prompt("Age: ").default("18").retries(2).get()?;
6    println!("Hello, {name} ({age}).");
7    Ok(())
8}
Source

pub fn retries(self, retries: usize) -> Self

Number of times to retry when parsing fails or input is empty w/o default.

Examples found in repository?
examples/basic.rs (line 5)
3fn main() -> Result<(), askit::Error> {
4    let name: String = prompt("Name: ").get()?;
5    let age: u8 = prompt("Age: ").default("18").retries(2).get()?;
6    println!("Hello, {name} ({age}).");
7    Ok(())
8}
Source

pub fn trim(self, yes: bool) -> Self

Whether to trim whitespace (default: true).

Source

pub fn to<T>(self) -> TypedPrompt<'a, T>
where T: FromStr, T::Err: Display,

Convert to a typed builder, enabling .default_val() and .validate().

Examples found in repository?
examples/default_typed.rs (line 6)
3fn main() -> Result<(), askit::Error> {
4    // Default tipado: não precisa parse
5    let port: u16 = prompt("Port [typed default=5432]: ")
6        .to()
7        .default_val(5432)
8        .get()?;
9
10    println!("Port = {port}");
11    Ok(())
12}
More examples
Hide additional examples
examples/validate.rs (line 5)
3fn main() -> Result<(), askit::Error> {
4    let port: u32 = prompt("Port: ")
5        .to()
6        .retries(2)
7        .validate(|p| (1..=65535).contains(p))
8        .message("Port must be in 1..=65535")
9        .get()?;
10
11    println!("Using port {port}");
12    Ok(())
13}
Source

pub fn get<T>(&self) -> Result<T, Error>
where T: FromStr, T::Err: Display,

Read from stdin, parse and return the desired type.

Examples found in repository?
examples/basic.rs (line 4)
3fn main() -> Result<(), askit::Error> {
4    let name: String = prompt("Name: ").get()?;
5    let age: u8 = prompt("Age: ").default("18").retries(2).get()?;
6    println!("Hello, {name} ({age}).");
7    Ok(())
8}
Source

pub fn get_with<T, R, W>( &self, reader: &mut R, writer: &mut W, ) -> Result<T, Error>
where T: FromStr, T::Err: Display, R: BufRead, W: Write,

Same as get(), but allows providing a custom reader and writer.

Auto Trait Implementations§

§

impl<'a> Freeze for Prompt<'a>

§

impl<'a> RefUnwindSafe for Prompt<'a>

§

impl<'a> Send for Prompt<'a>

§

impl<'a> Sync for Prompt<'a>

§

impl<'a> Unpin for Prompt<'a>

§

impl<'a> UnwindSafe for Prompt<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.