Crate clot

Source
Expand description

An opinionated mini argument parsing library that doesn’t use macros.

§Getting Started

use clot::{Clot, Opts};

fn main() {
    Clot::new("Example program")
        .cmd("hello", hello)
        .cmd("add", add)
        .execute()
}

fn add() -> Clot<impl Opts> {
    Clot::new("Add two numbers").run(run_add)
}

fn hello() -> Clot<impl Opts> {
    Clot::new("Print hello world").run(run_hello)
}

fn run_hello(_opts: &dyn Opts) {
    println!("Hello, world!");
}

fn run_add(_opts: &dyn Opts) {
    println!("Adding");
}

§Rules

Clot is opinionated on how you structure CLI arguments. This is how they work:

There are exactly four types of arguments:

All command line programs must accept the command

  • --help — Print help

If there are no fields, they must also accept

  • help — Alias to --help

§Commands

Commands are a named subtree of CLI options. Command names should be lowercase alphabetic ASCII without numbers, using - for word separation. No more than 3 words should be used, and 3 words should only if absolutely necessary.

Commands must start with -- if there are optional or required fields, otherwise they must begin with an alphabetic character. --help is special in that it’s required regardless of if there are fields.

Lists of commands are also possible.

--help
analyze
[exec <STMT>]
    exec 'a = 0' exec 'a += 1'

§Fields

Fields are positional arguments passed in to the program.

<INT>   Integer
    42
<PATH>  Path - Only time when UTF-8 compliance is optional, depending on OS
    ~/my-files/something.text

§Parameters

Parameters are named arguments that can be passed in. They must be a single word, using only alphabetic ASCII without numbers. List parameters are defined by defining the parameter multiple times.

§Examples

--verbosity {0…3}   Set verbosity level
    --verbosity 0
    --verbosity 1
    --verbosity 2
    --verbosity 3
--ratio <n>:<d>     Set ratio
    --ratio 1:2
    --ratio 5.3:7.1
[--append value]    Append a value
    --append 'book' --append 'car'

§Flags

Flags are single character lowercase ascii command line arguments that start with -. Multiple can be combined together. Each flag may appear at most once.

§Examples

-v      Verbose
-f      Force
-vf     Verbose and force

Modules§

cmds
Common commands
flags
Common flags
params
Common parameters

Structs§

Clot
Command line option tree / subtree

Traits§

Opts
A sealed trait implemented on the generic of Clot.