clap_fmt 0.1.1

Serialize a clap arg parser into the command-line arguments.
Documentation
  • Coverage
  • 75%
    3 out of 4 items documented0 out of 3 items with examples
  • Size
  • Source code size: 63.8 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 378.7 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 19s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • nobane/clap_fmt
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • nobane

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