#command-line #pretty #parse #parser #cli-parser #terminal

scalp

A declarative parsing library for pretty and highly customizable command-line interfaces

13 unstable releases (3 breaking)

0.4.3 Mar 31, 2024
0.4.2 Mar 30, 2024
0.4.0 Feb 28, 2024
0.3.1 Feb 24, 2024
0.1.1 Feb 2, 2024

#365 in Command-line interface

Download history 6/week @ 2024-02-01 565/week @ 2024-02-15 439/week @ 2024-02-22 100/week @ 2024-02-29 140/week @ 2024-03-07 18/week @ 2024-03-14 364/week @ 2024-03-28 23/week @ 2024-04-04

387 downloads per month

MIT license

140KB
4K SLoC

scalp


A declarative parsing library for pretty and highly customizable command-line interfaces. It provides a composable and extensible `Parse` trait that ensures comparative performance to a macro-full approach while offering greater flexibility and understandability.

Less magic, more control, same speed.


Getting Started

extern crate scalp;

use std::fs;
use scalp::*;

fn main() -> Result<(), Error> {
    #[derive(Debug, PartialEq, Eq)]
    enum Command {
        Run { settings: Option<String>, path: String },
        Show,
    }

    struct Root {
        debug: bool,
        yes: bool,
        force: bool,
        recurse: bool,
        command: Command,
    }

    let parser = Parser::builder()
        .case(Case::Kebab { upper: false })
        .option(|option| option.name("d").name("debug").help("Debug mode.").default(false))
        .option(|option| option.name("y").name("yes").swizzle().default(false))
        .option(|option| option.name("f").name("force").swizzle().default(false))
        .option(|option| option.name("r").name("recurse").swizzle().default(false))
        .options([Options::version(true, true), Options::help(true, true)])
        .group(|group| group
            .verb(|verb| verb.name("run")
                .usage("example run [OPTIONS]")
                .option(|option| option.position().require())
                .option(|option| option.name("s").name("settings").parse::<String>().map(|path| fs::read_to_string(path?).ok()))
                .map(|(file, settings)| Command::Run { path: file, settings }))
            .verb(|verb| verb.name("show").map(|_| Command::Show))
            .any()
            .require()
        )
        .map(|(debug, yes, force, recurse, command)| Root { debug, yes, recurse, force, command })
        .line()
        .note("Documentation: https://docs.rs/scalp/latest/scalp/")
        .build()?;

    let root = parser.parse_with(["--debug", "-fyr", "run", "./", "-s", "./settings.json"], [("", "")])?;
    assert!(root.debug);
    assert!(root.force);
    assert!(root.yes);
    assert!(root.recurse);

    let Command::Run { path, .. } = root.command else { panic!(); };
    assert_eq!(path, "./");
    Ok(())
}

Dependencies

~2.3–4.5MB
~76K SLoC