Question

Struct Question 

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

A Question that can be asked.

There are 12 variants.

Every Question has 4 common options.

  • name (required): This is used as the key in Answers. It is not shown to the user unless message is unspecified.

  • message: The message to display when the prompt is rendered in the terminal. If it is not given, the message defaults to “<name>: “. It is recommended to set this as name is meant to be a programmatic id.

  • when: Whether to ask the question or not. This can be used to have context based questions. If it is not given, it defaults to true.

  • ask_if_answered: Prompt the question even if it is answered. By default if an answer with the given name already exists, the question will be skipped. This can be override by setting ask_if_answered is set to true.

A Question can be asked by creating a PromptModule or using prompt_one or prompt_one_with.

§Examples

use requestty::Question;

let question = Question::input("name")
    .message("What is your name?")
    .default("John Doe")
    .transform(|name, previous_answers, backend| {
        write!(backend, "Hello, {}!", name)
    })
    .build();

Implementations§

Source§

impl Question<'static>

Source

pub fn input<N: Into<String>>(name: N) -> InputBuilder<'static>

Prompt that takes user input and returns a String

See the various methods on the builder for more details on each available option.

§Examples
use requestty::Question;

let input = Question::input("name")
    .message("What is your name?")
    .default("John Doe")
    .transform(|name, previous_answers, backend| {
        write!(backend, "Hello, {}!", name)
    })
    .build();
Source

pub fn password<N: Into<String>>(name: N) -> PasswordBuilder<'static>

Prompt that takes user input and hides it.

How it looks if you set a mask:

How it looks if you do not set a mask:

See the various methods on the builder for more details on each available option.

§Examples
use requestty::Question;

let password = Question::password("password")
    .message("What is your password?")
    .mask('*')
    .build();
Source

pub fn editor<N: Into<String>>(name: N) -> EditorBuilder<'static>

Prompt that takes launches the users preferred editor on a temporary file

Once the user exits their editor, the contents of the temporary file are read in as the result. The editor to use can be specified by the editor method. If unspecified, the editor is determined by the $VISUAL or $EDITOR environment variables. If neither of those are present, vim (for unix) or notepad (for windows) is used.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::Question;

let editor = Question::editor("description")
    .message("Please enter a short description about yourself")
    .extension(".md")
    .build();
Source

pub fn confirm<N: Into<String>>(name: N) -> ConfirmBuilder<'static>

Prompt that returns true or false.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::Question;

let confirm = Question::confirm("anonymous")
    .message("Do you want to remain anonymous?")
    .build();
Source

pub fn int<N: Into<String>>(name: N) -> IntBuilder<'static>

Prompt that takes a i64 as input.

The number is parsed using from_str.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::Question;

let int = Question::int("age")
    .message("What is your age?")
    .validate(|age, previous_answers| {
        if age > 0 && age < 130 {
            Ok(())
        } else {
            Err(format!("You cannot be {} years old!", age))
        }
    })
    .build();
Source

pub fn float<N: Into<String>>(name: N) -> FloatBuilder<'static>

Prompt that takes a f64 as input.

The number is parsed using from_str, but cannot be NaN.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::Question;

let float = Question::float("number")
    .message("What is your favourite number?")
    .validate(|num, previous_answers| {
        if num.is_finite() {
            Ok(())
        } else {
            Err("Please enter a finite number".to_owned())
        }
    })
    .build();
Source

pub fn expand<N: Into<String>>(name: N) -> ExpandBuilder<'static>

Prompt that allows the user to select from a list of options by key

The keys are ascii case-insensitive characters. The ‘h’ option is added by the prompt and shouldn’t be defined.

The choices are represented with the Choice enum. Choice::Choice can be multi-line, but Choice::Separators can only be single line.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::Question;

let expand = Question::expand("overwrite")
    .message("Conflict on `file.rs`")
    .choices(vec![
        ('y', "Overwrite"),
        ('a', "Overwrite this one and all next"),
        ('d', "Show diff"),
    ])
    .default_separator()
    .choice('x', "Abort")
    .build();
Source

pub fn select<N: Into<String>>(name: N) -> SelectBuilder<'static>

Prompt that allows the user to select from a list of options

The choices are represented with the Choice enum. Choice::Choice can be multi-line, but Choice::Separators can only be single line.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::{Question, DefaultSeparator};

let select = Question::select("theme")
    .message("What do you want to do?")
    .choices(vec![
        "Order a pizza".into(),
        "Make a reservation".into(),
        DefaultSeparator,
        "Ask for opening hours".into(),
        "Contact support".into(),
        "Talk to the receptionist".into(),
    ])
    .build();
Source

pub fn raw_select<N: Into<String>>(name: N) -> RawSelectBuilder<'static>

Prompt that allows the user to select from a list of options with indices

The choices are represented with the Choice enum. Choice::Choice can be multi-line, but Choice::Separators can only be single line.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::{Question, DefaultSeparator};

let raw_select = Question::raw_select("theme")
    .message("What do you want to do?")
    .choices(vec![
        "Order a pizza".into(),
        "Make a reservation".into(),
        DefaultSeparator,
        "Ask for opening hours".into(),
        "Contact support".into(),
        "Talk to the receptionist".into(),
    ])
    .build();
Source

pub fn multi_select<N: Into<String>>(name: N) -> MultiSelectBuilder<'static>

Prompt that allows the user to select multiple items from a list of options

Unlike the other list based prompts, this has a per choice boolean default.

The choices are represented with the Choice enum. Choice::Choice can be multi-line, but Choice::Separators can only be single line.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::{Question, DefaultSeparator};

let multi_select = Question::multi_select("cheese")
    .message("What cheese do you want?")
    .choice_with_default("Mozzarella", true)
    .choices(vec![
        "Cheddar",
        "Parmesan",
    ])
    .build();
Source

pub fn order_select<N: Into<String>>(name: N) -> OrderSelectBuilder<'static>

Prompt that allows the user to organize a list of options.

The choices are Strings and can be multiline.

See the various methods on the builder for more details on each available option.

§Examples
use requestty::{Question, DefaultSeparator};

let multi_select = Question::order_select("tasks")
    .message("Please organize the tasks to be done at home")
    .choices(vec![
        "Make the bed",
        "Clean the dishes",
        "Mow the lawn",
    ])
    .build();
Source

pub fn custom<'a, N, P>(name: N, prompt: P) -> CustomPromptBuilder<'a>
where N: Into<String>, P: Prompt + 'a,

Create a Question from a custom prompt.

See Prompt for more information on writing custom prompts and the various methods on the builder for more details on each available option.

§Examples
use requestty::{prompt, Question};

#[derive(Debug)]
struct MyPrompt { /* ... */ }


impl prompt::Prompt for MyPrompt {
    fn ask(
        self,
        message: String,
        answers: &prompt::Answers,
        backend: &mut dyn prompt::Backend,
        events: &mut dyn prompt::EventIterator,
    ) -> requestty::Result<Option<prompt::Answer>> {
        /* ... */
    }
}

let prompt = Question::custom("my-prompt", MyPrompt::new())
    .message("Hello from MyPrompt!")
    .build();

Trait Implementations§

Source§

impl<'a> Debug for Question<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<ConfirmBuilder<'a>> for Question<'a>

Source§

fn from(builder: ConfirmBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<CustomPromptBuilder<'a>> for Question<'a>

Source§

fn from(builder: CustomPromptBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<EditorBuilder<'a>> for Question<'a>

Source§

fn from(builder: EditorBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<ExpandBuilder<'a>> for Question<'a>

Source§

fn from(builder: ExpandBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<FloatBuilder<'a>> for Question<'a>

Source§

fn from(builder: FloatBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<InputBuilder<'a>> for Question<'a>

Source§

fn from(builder: InputBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<IntBuilder<'a>> for Question<'a>

Source§

fn from(builder: IntBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<MultiSelectBuilder<'a>> for Question<'a>

Source§

fn from(builder: MultiSelectBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<OrderSelectBuilder<'a>> for Question<'a>

Source§

fn from(builder: OrderSelectBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<PasswordBuilder<'a>> for Question<'a>

Source§

fn from(builder: PasswordBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<RawSelectBuilder<'a>> for Question<'a>

Source§

fn from(builder: RawSelectBuilder<'a>) -> Self

Consumes the builder returning a Question

Source§

impl<'a> From<SelectBuilder<'a>> for Question<'a>

Source§

fn from(builder: SelectBuilder<'a>) -> Self

Consumes the builder returning a Question

Auto Trait Implementations§

§

impl<'a> Freeze for Question<'a>

§

impl<'a> !RefUnwindSafe for Question<'a>

§

impl<'a> !Send for Question<'a>

§

impl<'a> !Sync for Question<'a>

§

impl<'a> Unpin for Question<'a>

§

impl<'a> !UnwindSafe for Question<'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.