xur_

> module

std::cli

argument parsing and interactive prompts

use std::cli; feature cli native only

std::cli parses the program's command-line arguments with pico-args, draws tables, and asks questions on the terminal with inquire. The arguments are the ones after the script's path (see std::argv).

> functions

# args() -> Args

A fresh Args holding all of the program's arguments, to take apart piece by piece.

# confirm(message: str) -> bool

Asks a yes/no question.

# editor(message: str) -> str

Opens an editor ($EDITOR, else $VISUAL, else nano or notepad) and returns the text saved in it.

# multi_select(message: str, options: list<str>) -> list<str>

Asks the user to pick any number of options.

# parse(specs: list<list<str>>) -> Result<record, str>

Parses all the arguments at once. Each spec is [long, kind] or [long, short, kind], such as ["--out", "-o", "option"]. kind is "flag" (a bool), "option" (an Option<str>) or "value" (a required str). The record has a field per spec, named after the long form without its dashes and with - turned into _ (--dry-run gives dry_run), and an empty rest list. Returns Err naming any arguments left over. A missing required value or an option without its value is a runtime error.

# password(message: str) -> str

Asks for text without showing it, and without asking twice.

# prompt(message: str) -> str

Asks for a line of text.

# select(message: str, options: list<str>) -> str

Asks the user to pick one of options, which must not be empty.

# table(headers: list<any>, rows: list<list<any>>) -> str

A table with rounded box-drawing borders, as a string to print. headers is optional: called with one list, it is the rows. Each cell is shown as by {}.

> types

┌ type Args

Each method consumes what it finds, so later calls don't see it. A flag is named by one spelling, such as "--verbose", or by two, a short and a long one in either order, as in args.option("--output", "-o"). A name that isn't -x or --name is a runtime error.

# args.contains(key: str, short?: str) -> bool

Whether the flag is present anywhere among the arguments.

# args.finish() -> Result<(), Error>

Checks that nothing is left: Err(cli::Error::Unexpected(names)) if something is.

# args.free() -> Result<Option<str>, Error>

The next remaining argument, whatever it is, or None if none are left. Call it after taking the flags and options.

# args.option(key: str, short?: str) -> Option<str>

The value after the option, as in --output out.txt, or None if it isn't given. The option without a value is a runtime error.

# args.remaining() -> list<str>

Takes all the arguments that are left.

# args.subcommand() -> Result<Option<str>, Error>

The first remaining argument, if it doesn't start with -; otherwise None.

# args.value(key: str, short?: str) -> Result<str, Error>

Like option, but a missing option is Err(cli::Error::Arg(message)).

┌ enum Error

A recoverable CLI-parsing error. Match it as `Err(cli::Error::Unexpected(msg))`.

Error::Arg(str)An argument could not be read (missing required value, bad encoding, …).
Error::Unexpected(str)Unexpected leftover arguments after parsing.

> prompts

Each prompt waits for the user. If the user cancels (Esc or Ctrl-C) or stdin isn't a terminal, it is a runtime error.

> errors

cli::Error is a matchable enum: Arg(str) for a missing or unreadable argument, and Unexpected(str) for arguments left over at finish.

> examples

use std::cli;
use std::io;

fn main() -> Result<()> {
    let args = cli::args();
    let verbose = args.contains("--verbose");
    let output = args.option("--output");
    let input = args.free()?;
    args.finish()?;
    match input {
        Some(file) => io::println("processing: {}", file),
        None => io::println("no input file given"),
    }
    Ok(())
}