8 releases

0.3.1 Jun 8, 2022
0.3.0 Feb 6, 2020
0.2.0 Dec 14, 2019
0.1.5 Jul 29, 2018
0.1.4 May 31, 2018

#401 in Command-line interface

Download history 12267/week @ 2024-07-23 9577/week @ 2024-07-30 10663/week @ 2024-08-06 9850/week @ 2024-08-13 12220/week @ 2024-08-20 14058/week @ 2024-08-27 14426/week @ 2024-09-03 11697/week @ 2024-09-10 11751/week @ 2024-09-17 11641/week @ 2024-09-24 13152/week @ 2024-10-01 13218/week @ 2024-10-08 13755/week @ 2024-10-15 12810/week @ 2024-10-22 14108/week @ 2024-10-29 13249/week @ 2024-11-05

56,070 downloads per month
Used in 35 crates (29 directly)

MIT license

19KB
281 lines

promptly

A simple, opinionated prompting library

Features include:

  • Re-prompt until valid
  • Prompts for several types, and extensible
  • Sane handling of escapes via rustyline
  • Path completion when prompting for paths
  • Dead simple to use. Perhaps too simple.

Usage

Simply call prompt or prompt_default to prompt for any Promptable type:

  • prompt(msg) - prompt until input can be parsed as the inferred return type. Re-prompts if input is empty.
  • prompt_opt(msg) - prompt until input can be parsed as the inferred return type. Returns None if input is empty.
  • prompt_default(msg, default) - prompt until input can be parsed as the inferred return type. Uses default value if input is empty.
use promptly::{prompt, prompt_default, prompt_opt};

// Prompt until a non-empty string is provided
let name: String = prompt("Enter your name")?;

// Prompt for other `FromStr` types
let age: u32 = prompt("Enter your age")?;

// Prompt for optional paths with path completion. Returns `None` if empty input.
let photo: Option<PathBuf> = prompt_opt("Enter a path to a profile picture")?;

// Prompt Y/n with a default value when input is empty
let fallback = prompt_default("Would you like to receive marketing emails", true);

// Prompt for a url using the url crate (requires either 'nightly' or 'url' feature)
let website: Url = prompt("Enter a website URL");

More...

The API surface of this crate is opinionated and experimental, but open to fresh ideas.

Dependencies

~5–18MB
~205K SLoC