An attribute macro to simplify writing simple command line applications.

Overview

fncli

An attribute macro to simplify writing simple command line applications.

Example

#[fncli::cli]
fn main(a: i32, b: i32) {
    println!("{}", a + b);
}
$ cargo run 1 2
3
$ cargo run 1
missing argument: `b: i32`

USAGE:
    target/debug/examples/add <a: i32> <b: i32>
$ cargo run 1 2 3
too many arguments (expected 2)

USAGE:
    target/debug/examples/add <a: i32> <b: i32>
$ cargo run 1 a
failed to parse argument: `b: i32` (ParseIntError { kind: InvalidDigit })

USAGE:
    target/debug/examples/add <a: i32> <b: i32>

For a more complete example, see the time elapsed example.

How It Works

fn main() {
    let (a, b) = {
        let mut args = std::env::args();

        let cmd = args.next().expect("should have a command name");

        let exit = |err: &str| -> ! {
            eprintln!("{err}");
            eprintln!();
            eprintln!("USAGE:\n    {cmd} <a: i32> <b: i32>");
            std::process::exit(1)
        };

        let tuple = (
            i32::from_str(
                &args
                    .next()
                    .unwrap_or_else(|| exit("missing argument: `a: i32`")),
            )
            .unwrap_or_else(|e| exit(&format!("failed to parse argument `a: i32` ({e:?})"))),
            i32::from_str(
                &args
                    .next()
                    .unwrap_or_else(|| exit("missing argument: `b: i32`")),
            )
            .unwrap_or_else(|e| exit(&format!("failed to parse argument `b: i32` ({e:?})"))),
        );

        if args.next().is_some() {
            exit("too many arguments (expected 2)");
        }

        tuple
    };

    {
        println!("{}", a + b);
    }
}

The generated code is very simple, and not too different from how one would write the code by hand.

You might also like...
An html macro for dioxus applications.

dioxus html macro This crate offers an html! like macro for dioxus applications. It expands to the equivalent rsx! macro call you would have made othe

Rustato: A powerful, thread-safe global state management library for Rust applications, offering type-safe, reactive state handling with an easy-to-use macro-based API.
Rustato: A powerful, thread-safe global state management library for Rust applications, offering type-safe, reactive state handling with an easy-to-use macro-based API.

Rustato State Manager A generical thread-safe global state manager for Rust Introduction • Features • Installation • Usage • Advanced Usage • Api Refe

Rust-powered CLI tool designed to simplify and streamline the release process with help of ChatGPT
Rust-powered CLI tool designed to simplify and streamline the release process with help of ChatGPT

$ releasecraftsman 👷🏻‍♂️🛠 Automate Your Release Process with Precision and Ease. 🎁 Features Generate well-crafted release notes using GPT-3.5 and

Wrapper over MMTk to simplify integration with runtimes: thread management system, object header, root scanning

vmkit A library which provides bunch of building blocks to make a VM in Rust. Feautures MMTK integration out of the box Thread management provided by

Command-line HTTP client for sending a POST request to specified URI on each stdin line.

line2httppost Simple tool to read lines from stdin and post each line as separate POST request to a specified URL (TCP connection is reused though). G

Pink is a command-line tool inspired by the Unix man command.

Pink is a command-line tool inspired by the Unix man command. It displays custom-formatted text pages in the terminal using a subset of HTML-like tags.

Enhance Rust errors with file and line details using the `#[wherr]` macro for clearer debugging.

wherr Crate Discuss wherr on Hacker News Enhance Rust's ? operator by appending file and line number details to errors, simplifying the debugging proc

argmax is a library that allows Rust applications to avoid Argument list too long errors (E2BIG) by providing a std::process::Command wrapper with a

argmax argmax is a library that allows Rust applications to avoid Argument list too long errors (E2BIG) by providing a std::process::Command wrapper w

Cork is a simple command-line calculator, mainly targeted towards people who deal with hex numbers
Cork is a simple command-line calculator, mainly targeted towards people who deal with hex numbers

Cork is a simple command-line calculator, mainly targeted towards people who deal with hex numbers. It deals only with integer arithmetic. Expressions may involve mixed bases (limited to decimal, hexadecimal, octal and binary numbers). The global output format may be set to a particular radix - by default it is hex.

Owner
Vidhan Bhatt
he/they. uni student. rustacean, gopher, & pythonista.
Vidhan Bhatt
A Rust command line tool to simplify embedded development and deployment.

Bobbin-CLI bobbin-cli is a tool designed to make it easy to build, deploy, test and debug embedded devices using a unified CLI. bobbin-cli understands

Bobbin 110 Dec 25, 2022
KAIVM is a multiplatform Command Line Interface (CLI) designed to simplify the process of downloading, managing, configuring, and running different versions of Shinkai Node

KAIVM - Shinkai Version Manager KAIVM is a multiplatform Command Line Interface (CLI) designed to simplify the process of downloading, managing, confi

Alfredo Gallardo 7 May 1, 2024
Command-line tool designed to simplify the process of managing multiple .NET SDK versions on your system

.NET Version Manager (dver) Overview dver is a command-line tool designed to simplify the process of managing multiple .NET SDK versions on your syste

Sergio Triana Escobedo 5 Aug 23, 2024
A dead simple functional testing tool for command line applications

Pharaoh : build that test pyramid! What it is Pharaoh is a dead simple, no permission needed, functional test runner for command line applications, wr

Kevin Sztern 17 Dec 13, 2021
fas stand for Find all stuff and it's a go app that simplify the find command and allow you to easily search everything you nedd

fas fas stands for Find all stuff and it's a rust app that simplify the find command and allow you to easily search everything you need. Note: current

M4jrT0m 1 Dec 24, 2021
Command-line tool that provides a workflow for extending, editing, diffing, and writing to vim-style grep lines.

Grug Grug is a command-line tool that provides a workflow for expanding, editing, diffing, and writing edits to files using vim-styled grep lines (suc

null 4 Apr 25, 2023
Small command-line tool to switch monitor inputs from command line

swmon Small command-line tool to switch monitor inputs from command line Installation git clone https://github.com/cr1901/swmon cargo install --path .

William D. Jones 5 Aug 20, 2022
Solving context limits when working with AI LLM models by implementing a "chunkable" attribute on your prompt structs.

Promptize Promptize attempts to solve the issues with context limits when working with AI systems. It allows a user to add an attribute to their struc

Dan Nelson 5 Jul 18, 2023
Upkeep your websites and web applications with ease from the comfort of the command line.

Upkeep Upkeep your websites and web applications with ease from the comfort of the command line. Explore the docs » View Demo · Report Bug · Request F

Kevin B 0 Dec 24, 2021
CarLI is a framework for creating single-command and multi-command CLI applications in Rust

CarLI is a framework for creating single-command and multi-command CLI applications in Rust. The framework provides error and IO types better suited for the command line environment, especially in cases where unit testing is needed.

Kevin Herrera 3 Jan 21, 2022