5 releases

0.1.4 Nov 29, 2024
0.1.3 Nov 27, 2024
0.1.2 Nov 25, 2024
0.1.1 Nov 25, 2024
0.1.0 Nov 25, 2024

#264 in Procedural macros

Download history 427/week @ 2024-11-25 26/week @ 2024-12-02 33/week @ 2024-12-09

486 downloads per month

MIT license

5KB

Terse CLI

A wrapper around clap that lets you build CLI applications with very little boilerplate code.

Modeled after tiangolo's typer python library, you simply define your commands and subcommands as functions and annotate them with the #[command] attribute.

Current Status: Alpha

This is a work in progress. The core functionality is implemented, but if you want any customization on how your CLI is used (e.g. positional arguments, custom help messages, etc.) those things are not yet supported.

Known issues:

  • every command must have a return type that implements Display
  • positional arguments are not yet supported
  • argument docs are not yet supported

Installation

Install both clap and terse_cli:

$ cargo add clap --features derive
$ cargo add terse_cli

Example

Below snippet is from ./example/main.rs.

use clap::Parser;
use terse_cli::{command, subcommands};

/// Example: cargo run -- command-one --a 3
#[command]
fn command_one(a: i32, b: Option<i32>) -> i32 {
    a + b.unwrap_or(0)
}

/// Example: cargo run -- my-subcommands command-two --name Bob
#[command]
fn command_two(name: String) -> String {
    format!("hello {}", name)
}

/// Example: cargo run -- my-subcommands command-three --a 7 --b 3
#[command]
fn command_three(a: i32, b: i32) -> String {
    format!("the difference is {}", a - b)
}

/// Example: cargo run -- my-subcommands command-four
#[command]
fn command_four() -> String {
    "command four".to_string()
}

subcommands!(
    /// These are the subcommands
    my_subcommands, [command_two, command_three, command_four]);

subcommands!(
    /// Example docs for the "root"
    cli, [command_one, my_subcommands]);

fn main() {
    cli::run(cli::Args::parse());
}

// you can also use `--help` as you would expect
// Example: cargo run -- my-subcommands --help

Dependencies

~0.6–1.1MB
~21K SLoC