2 releases

Uses new Rust 2024

new 0.1.1 May 17, 2025
0.1.0 May 15, 2025

#244 in Command-line interface

41 downloads per month

MIT license

83KB
1.5K SLoC

ineed

ineed is a lightweight CLI prompting Rust crate.

It provides utility traits and types to prompt values in a more convenient way, and allows you to customize the style of the prompts.

Usage

First, add the crate to your dependency:

cargo add ineed

Then, add this line at the beginning of your code:

use ineed::prelude::*;

You are ready to use this crate. If you need a written value of any type, let's say u8, you can just ask for it:

let age = ineed::written::<u8>("How old are you?").prompt().unwrap();

ineed will manage the prompt format for you, and the input checking.

This example prints something similar to this:

- How old are you?
>

Features

You want to customize the prompt? Use the .fmt(...) method like this:

let age = ineed::written::<u8>("How old are you?")
    .fmt(ineed::fmt().input_prefix(">> "))
    .prompt()
    .unwrap();
println!("You are {age}!");

And the last line will be >> instead of > from the previous example.

You can also ask for a selectable value:

enum LicenseType {
    MIT,
    GPL,
    BSD,
}

let license = ineed::selected("The license type", [
    ("MIT", LicenseType::MIT),
    ("GPL", LicenseType::GPL),
    ("BSD", LicenseType::BSD),
])
    .prompt()
    .unwrap();

Or compose the prompts into one binding:

#[derive(Debug)]
enum Age {
    Minor,
    LegalAge,
    Unknown,
}

let (name, age) = ineed::written::<String>("Your name")
    .then(
        ineed::written::<u8>("How old are you?")
            .max_tries(3)
            .map(|age| match age {
                Ok(..18) => Age::Minor,
                Ok(_) => Age::LegalAge,
                Err(_exceeded) => Age::Unknown,
            })
    )
    .prompt()
    .unwrap();

println!("Your name is {name} and your age status is {age:?}");

There are many other promptable types, which are listed in the crate documentation.

You can also prompt for password. For this, you must add the rpassword feature:

cargo add ineed -F rpassword

which will give you access to the ineed::password promptable.

You can find more examples in the /examples folder.

Dependencies

~0–6.5MB
~31K SLoC