Skip to content

JeffrayZhang/terse-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages