Forms for the terminal: text fields parsed into any type, optional fields, and choices of one or several options moved through with the arrow keys.
cargo add form_builderEach kind of field has its own builder, added to the form with field:
use form_builder::validate::{self, Validator};
use form_builder::{Confirm, Error, FormBuilder, MultiSelect, Password, Select, Text};
fn main() -> Result<(), Error> {
let mut form = FormBuilder::new()
.field(Text::<String>::new("name", "Name:").validator(
Validator::new().rule(validate::name, "Name cannot contain digits"),
))
.field(Text::<u16>::new("port", "Port:").default(8080).hint("1-65535"))
.field(Text::<f64>::optional("height", "Height in metres:"))
.field(Select::new("plan", "Plan:", [(1u8, "Free"), (2, "Pro")]).default(&1))
.field(MultiSelect::new("tags", "Tags:", [("rust", "Rust"), ("go", "Go")]).limit(2))
.field(Confirm::new("company", "Signing up for a company?").default(false))
.field(Text::<String>::new("vat", "VAT ID:").when(|answers| answers.is("company", &true)))
.field(Password::new("password", "Password:").repeat("Repeat the password:"))
.build();
form.fill()?;
let name: String = form.get("name")?;
let port: u16 = form.get("port")?;
let height: Option<f64> = form.get("height")?;
let tags: Vec<&str> = form.get("tags")?;
println!("{name}, {port}, {height:?}, {tags:?}");
Ok(())
}With the derive feature, #[derive(Form)] asks for a struct field by field. The examples run a longer form, a form
answered from the environment and the arguments, and a derived struct:
cargo run --example demo
APP_NAME=Ann cargo run --example preset -- port=9000 plan=pro
cargo run --example derive --features derive- Contents – every document of the library, and what it covers.
- Features – every kind of field, defaults, choices, validation, languages and errors.
- Standards – the specifications the checks and the terminal follow.
- Changelog – every release, and what it changed.
- Contributing – reporting a bug, proposing a field, and sending a change.
- Security – reporting a vulnerability privately.
- API documentation – every type and method, on docs.rs.
This project is licensed under the MIT License. See the LICENSE file for more details.
form_builder depends on crossterm, under MIT, and on
regex and thiserror, under MIT or
Apache-2.0, at your choice. Cargo fetches them with their own license files; a program that ships form_builder
compiled in ships them too, and includes their licenses with it.