| Crates.io | clap_fmt |
| lib.rs | clap_fmt |
| version | 0.1.1 |
| created_at | 2025-09-22 08:35:19.119254+00 |
| updated_at | 2025-09-22 08:42:44.235053+00 |
| description | Serialize a clap arg parser into the command-line arguments. |
| homepage | |
| repository | https://github.com/nobane/clap_fmt |
| max_upload_size | |
| id | 1849674 |
| size | 63,817 |
Serialize clap structs back into command-line arguments.
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.
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");
}
Supports all common clap argument types:
-vvv)Handles default values correctly (omits them from output)
Preserves argument order semantics
Works with both short (-v) and long (--verbose) arguments
MIT