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>
impl<'a> Choice<'a>
Sourcepub fn new(prompt: &'a str, choices: &'a [&'a str]) -> Self
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}Sourcepub fn default(self, default: &'a str) -> Self
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}Sourcepub fn show_default(self, show: bool) -> Self
pub fn show_default(self, show: bool) -> Self
Controls whether the default choice is marked with * in the suffix list.
Sourcepub fn show_choices(self, show: bool) -> Self
pub fn show_choices(self, show: bool) -> Self
Controls whether the full choice list is appended to the prompt as [a/b/c].
Sourcepub fn ask(self) -> String
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more