clap_fmt

Crates.ioclap_fmt
lib.rsclap_fmt
version0.1.1
created_at2025-09-22 08:35:19.119254+00
updated_at2025-09-22 08:42:44.235053+00
descriptionSerialize a clap arg parser into the command-line arguments.
homepage
repositoryhttps://github.com/nobane/clap_fmt
max_upload_size
id1849674
size63,817
(nobane)

documentation

README

clap_fmt

Serialize clap structs back into command-line arguments.

Overview

clap_fmt allows you to take a struct that derives clap::Parser and serialize it back into the command-line arguments that would produce that struct.

Usage

Add to your Cargo.toml:

[dependencies]
clap = "4"
clap_fmt = "0.1"
serde = { version = "1", features = ["derive"] }

FmtArgs on your clap struct:

use clap::Parser;
use clap_fmt::FmtArgs as _;
use serde::Serialize;

#[derive(Parser, Debug, Serialize)]
#[command(name = "myapp")]
struct Cli {
    #[arg(short, long)]
    verbose: bool,

    #[arg(short, long)]
    output: Option<String>,

    #[arg(short, long, default_value = "input.txt")]
    input: String,

    files: Vec<String>,
}

fn main() {
    let cli = Cli {
        verbose: true,
        output: Some("out.txt".to_string()),
        input: "data.txt".to_string(),
        files: vec!["file1.rs".to_string(), "file2.rs".to_string()],
    };

    // Get just the arguments
    let args = cli.to_args();
    assert_eq!(args, vec!["--verbose", "--output", "out.txt", "--input", "data.txt", "file1.rs", "file2.rs"]);

    // Get arguments with program name
    let args_with_program = cli.to_args_with_program("myapp");
    assert_eq!(args_with_program[0], "myapp");
}

Features

  • Supports all common clap argument types:

    • Boolean flags
    • Options with values
    • Multiple values and repeated arguments
    • Positional arguments
    • Subcommands (including nested)
    • Count flags (-vvv)
    • Value enums
  • Handles default values correctly (omits them from output)

  • Preserves argument order semantics

  • Works with both short (-v) and long (--verbose) arguments

License

MIT

Commit count: 3

cargo fmt