| 1 |
<!-- omit in TOC --> |
| 2 |
# `clap_mangen` |
| 3 |
|
| 4 |
> **Manpage generation for `clap`** |
| 5 |
|
| 6 |
[](https://crates.io/crates/clap_mangen) |
| 7 |
[](https://crates.io/crates/clap_mangen) |
| 8 |
[](https://github.com/clap-rs/clap/blob/clap_mangen-v0.3.3/LICENSE-APACHE) |
| 9 |
[](https://github.com/clap-rs/clap/blob/clap_mangen-v0.3.3/LICENSE-MIT) |
| 10 |
|
| 11 |
Dual-licensed under [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT). |
| 12 |
|
| 13 |
1. [About](#about) |
| 14 |
2. [API Reference](https://docs.rs/clap_mangen) |
| 15 |
3. [Questions & Discussions](https://github.com/clap-rs/clap/discussions) |
| 16 |
4. [CONTRIBUTING](https://github.com/clap-rs/clap/blob/clap_mangen-v0.3.3/clap_mangen/CONTRIBUTING.md) |
| 17 |
5. [Sponsors](https://github.com/clap-rs/clap/blob/clap_mangen-v0.3.3/README.md#sponsors) |
| 18 |
|
| 19 |
## About |
| 20 |
|
| 21 |
Generate [ROFF](https://en.wikipedia.org/wiki/Roff_(software)) from a `clap::Command`. |
| 22 |
|
| 23 |
### Example |
| 24 |
|
| 25 |
We're going to assume you want to generate your man page as part of your |
| 26 |
development rather than your shipped program having a flag to generate it. |
| 27 |
|
| 28 |
Run |
| 29 |
```console |
| 30 |
$ cargo add --build clap_mangen |
| 31 |
``` |
| 32 |
|
| 33 |
In your `build.rs`: |
| 34 |
```rust,no_run |
| 35 |
fn main() -> std::io::Result<()> { |
| 36 |
let out_dir = std::path::PathBuf::from(std::env::var_os("OUT_DIR").ok_or(std::io::ErrorKind::NotFound)?); |
| 37 |
|
| 38 |
let cmd = clap::Command::new("mybin") |
| 39 |
.arg(clap::arg!(-n --name <NAME>)) |
| 40 |
.arg(clap::arg!(-c --count <NUM>)); |
| 41 |
|
| 42 |
let man = clap_mangen::Man::new(cmd); |
| 43 |
let mut buffer: Vec<u8> = Default::default(); |
| 44 |
man.render(&mut buffer)?; |
| 45 |
|
| 46 |
std::fs::write(out_dir.join("mybin.1"), buffer)?; |
| 47 |
|
| 48 |
Ok(()) |
| 49 |
} |
| 50 |
``` |
| 51 |
|
| 52 |
Tip: Consider a [cargo xtask](https://github.com/matklad/cargo-xtask) instead of a `build.rs` to reduce build costs. |