Skip to main content

Choice

Struct Choice 

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

Builder for a multiple-choice prompt.

Wraps Terminal::choice with a chainable configuration API. Call Choice::ask to display the prompt and block until the user selects a valid option.

§Example

use vecli::Choice;

let env = Choice::new("Select environment:", &["dev", "staging", "prod"])
    .default("dev")
    .ask();

Implementations§

Source§

impl<'a> Choice<'a>

Source

pub fn new(prompt: &'a str, choices: &'a [&'a str]) -> Self

Creates a new Choice with the given question text and choice list.

choices must not be empty. Both the default indicator and the inline choice list are shown by default.

Examples found in repository?
examples/taskr.rs (line 63)
51fn add(ctx: &CommandContext) {
52    // Demonstrates: positionals, per-command flags, strict flags, Choice prompt
53    let task = ctx
54        .positionals
55        .first()
56        .map(String::as_str)
57        .unwrap_or("<unnamed>");
58
59    let priority = if let Some(p) = ctx.flags.get("priority") {
60        p.clone()
61    } else {
62        // If --priority not passed, ask interactively
63        Choice::new("Priority:", &["low", "medium", "high"])
64            .default("medium")
65            .ask()
66    };
67
68    if ctx.flags.contains_key("verbose") {
69        println!(
70            "[verbose] add called with task='{}', priority='{}'",
71            task, priority
72        );
73    }
74
75    println!("Added task '{}' with priority {}.", task, priority);
76}
Source

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

Sets the default choice returned when the user presses Enter with no input.

Must be a value present in choices; validated at prompt time by Terminal::choice.

Examples found in repository?
examples/taskr.rs (line 64)
51fn add(ctx: &CommandContext) {
52    // Demonstrates: positionals, per-command flags, strict flags, Choice prompt
53    let task = ctx
54        .positionals
55        .first()
56        .map(String::as_str)
57        .unwrap_or("<unnamed>");
58
59    let priority = if let Some(p) = ctx.flags.get("priority") {
60        p.clone()
61    } else {
62        // If --priority not passed, ask interactively
63        Choice::new("Priority:", &["low", "medium", "high"])
64            .default("medium")
65            .ask()
66    };
67
68    if ctx.flags.contains_key("verbose") {
69        println!(
70            "[verbose] add called with task='{}', priority='{}'",
71            task, priority
72        );
73    }
74
75    println!("Added task '{}' with priority {}.", task, priority);
76}
Source

pub fn show_default(self, show: bool) -> Self

Controls whether the default choice is marked with * in the suffix list.

Source

pub fn show_choices(self, show: bool) -> Self

Controls whether the full choice list is appended to the prompt as [a/b/c].

Source

pub fn ask(self) -> String

Displays the prompt and returns the user’s selection as a lowercase string.

Examples found in repository?
examples/taskr.rs (line 65)
51fn add(ctx: &CommandContext) {
52    // Demonstrates: positionals, per-command flags, strict flags, Choice prompt
53    let task = ctx
54        .positionals
55        .first()
56        .map(String::as_str)
57        .unwrap_or("<unnamed>");
58
59    let priority = if let Some(p) = ctx.flags.get("priority") {
60        p.clone()
61    } else {
62        // If --priority not passed, ask interactively
63        Choice::new("Priority:", &["low", "medium", "high"])
64            .default("medium")
65            .ask()
66    };
67
68    if ctx.flags.contains_key("verbose") {
69        println!(
70            "[verbose] add called with task='{}', priority='{}'",
71            task, priority
72        );
73    }
74
75    println!("Added task '{}' with priority {}.", task, priority);
76}

Auto Trait Implementations§

§

impl<'a> Freeze for Choice<'a>

§

impl<'a> RefUnwindSafe for Choice<'a>

§

impl<'a> Send for Choice<'a>

§

impl<'a> Sync for Choice<'a>

§

impl<'a> Unpin for Choice<'a>

§

impl<'a> UnsafeUnpin for Choice<'a>

§

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