# clap_fmt
Serialize [clap](https://github.com/clap-rs/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`:
```toml
[dependencies]
clap = "4"
clap_fmt = "0.1"
serde = { version = "1", features = ["derive"] }
```
`FmtArgs` on your clap struct:
```rust
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