Skip to content

Commit

Permalink
feat: Generate man page from command line parser
Browse files Browse the repository at this point in the history
  • Loading branch information
VorpalBlade committed Feb 29, 2024
1 parent fed6105 commit 959c7b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/paketkoll/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ proc-exit = "2.0.1"
[build-dependencies]
clap = { version = "4.5.1", features = ["derive"] }
clap_complete = "4.5.1"
clap_mangen = "0.2.20"
14 changes: 9 additions & 5 deletions crates/paketkoll/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ use clap::ValueEnum;
use clap_complete::{generate_to, Shell};
use std::env;
use std::io::Error;
use std::path::PathBuf;

include!("src/cli.rs");

fn main() -> Result<(), Error> {
let outdir = match env::var_os("OUT_DIR") {
None => return Ok(()),
Some(outdir) => outdir,
};
let outdir = env::var_os("OUT_DIR").ok_or(std::io::ErrorKind::NotFound)?;

let mut cmd = Cli::command();
for &shell in Shell::value_variants() {
generate_to(shell, &mut cmd, "paketkoll", &outdir)?;
}

let man = clap_mangen::Man::new(cmd);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;
std::fs::write(PathBuf::from(outdir).join(man.get_filename()), buffer)?;

// Outputs will be in a directory like target/release/build/paketkoll-<some-hash>/out/
// That is unfortunate, but there doesn't seem to be a way to get a stable output directory
// println!("cargo:warning=shell completion files generated in: {outdir:?}");
// println!("cargo:warning=shell completion & man page generated in: {outdir:?}");

Ok(())
}

0 comments on commit 959c7b1

Please sign in to comment.