9 releases (4 breaking)
0.5.1 | Jan 20, 2024 |
---|---|
0.5.0 | Jan 20, 2024 |
0.4.0 | Jan 20, 2024 |
0.3.0 | Jan 20, 2024 |
0.1.1 | Jan 18, 2024 |
#328 in Command-line interface
11KB
111 lines
promptis
A Rust crate to simplify getting user input in the CLI.
Examples
cargo run --example hello
: A basic Hello World program that reads the user's name
cargo run --example data
: A program that asks for a message and a number and repeats the message that many times
cargo run --example closing
: A program that demonstrates the user ending the program early with an input
Example Usage
// Prompt for the user's name and wait for them to respond with input
let name: String = Input::new().prompt("Enter your name: ").wait();
// Prompt the user for a number and wait for them to respond,
// displaying the error if they input something else
let id: u32 = Input::new()
.prompt("Enter a number: ")
.err_msg("Not a number; please retry")
.wait();
lib.rs
:
Simplify getting user input for your CLI applications
Check out the examples, too:
cargo run --example closing
demonstrates quitting the program earlycargo run --example data
demonstrates getting data from the usercargo run --example hello
is a basic hello world program
Example usage:
let name: String = Input::new()
.prompt("Enter your name: ")
.wait();
println!("Hello, {}!", name);
You can also set error messages for when the user messes up the input.
let number: i32 = Input::new()
.err_msg("That wasn't a number; please try again")
.prompt("Enter a number: ")
.wait();
println!("Your number is: {}", number);
You can choose to just get the first input, regardless of whether it's good.
let number: Option<i32> = Input::new()
.prompt("Enter a number: ")
.read();
match number {
Some(n) => println!("Your number is: {}"),
None => println!("You didn't enter a number!")
}
You can specify a keyword that will end the program when entered
let number: i32 = Input::new()
.quit("quit") // this can result in the program ending early
.prompt("Enter a number: ")
.wait();
println!("Your number is: {}", number);
You can re-use the same input object for multiple inputs.
let mut input = Input::new()
.err_msg("Unexpected input; please retry")
.quit("quit");
let name: String = input.prompt("Enter your name: ").wait();
let age: u32 = input.prompt("Enter your age: ").wait();
let weight: f64 = input.prompt("Enter your weight: ").wait();
println!("Name: {}\nAge: {}\nWeight: {}", name, age, weight);