diff --git a/Cargo.lock b/Cargo.lock index 221ae363..6e09c03a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,16 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "clap_mangen" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e503c3058af0a0854668ea01db55c622482a080092fede9dd2e00a00a9436504" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "cpufeatures" version = "0.2.5" @@ -440,6 +450,7 @@ dependencies = [ "build_uuid", "clap", "clap_complete", + "clap_mangen", "derive_builder", "im", "indexmap", @@ -620,6 +631,12 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "rust-embed" version = "6.4.2" diff --git a/Cargo.toml b/Cargo.toml index c152d094..4800def7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ url = "2.3.1" rust-embed = "6.4.2" task_partitioner = "0.1.1" clap_complete = "4.0.6" +clap_mangen = "0.2.5" [profile.release] diff --git a/src/cli.rs b/src/cli.rs index d9035f96..0ec6e4de 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -207,4 +207,16 @@ pub fn clap() -> clap::Command { ) .hide(true), ) + .subcommand( + Command::new("manpages") + .about("Generate laze manpages.") + .arg( + Arg::new("outdir") + .help("directory in which to create manpage files") + .value_parser(value_parser!(PathBuf)) + .value_hint(ValueHint::DirPath) + .required(true), + ) + .hide(true), + ) } diff --git a/src/main.rs b/src/main.rs index 6f1e298b..ae83b542 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,19 +119,54 @@ fn try_main() -> Result { // handle completion subcommand here, so the project specific // stuff is skipped - if let Some(("completion", matches)) = matches.subcommand() { - fn print_completions(gen: G, cmd: &mut clap::Command) { - clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); + match matches.subcommand() { + Some(("completion", matches)) => { + fn print_completions(gen: G, cmd: &mut clap::Command) { + clap_complete::generate( + gen, + cmd, + cmd.get_name().to_string(), + &mut std::io::stdout(), + ); + } + if let Some(generator) = matches + .get_one::("generator") + .copied() + { + let mut cmd = cli::clap(); + eprintln!("Generating completion file for {}...", generator); + print_completions(generator, &mut cmd); + } + return Ok(0); } - if let Some(generator) = matches - .get_one::("generator") - .copied() - { - let mut cmd = cli::clap(); - eprintln!("Generating completion file for {}...", generator); - print_completions(generator, &mut cmd); + Some(("manpages", matches)) => { + fn create_manpage(cmd: clap::Command, outfile: &Path) -> Result<(), Error> { + let man = clap_mangen::Man::new(cmd); + let mut buffer: Vec = Default::default(); + man.render(&mut buffer)?; + + std::fs::write(outfile, buffer)?; + Ok(()) + } + let mut outpath: PathBuf = matches.get_one::("outdir").unwrap().clone(); + let cmd = cli::clap(); + + outpath.push("laze.1"); + create_manpage(cmd.clone(), &outpath)?; + + for subcommand in cmd.get_subcommands() { + if subcommand.is_hide_set() { + continue; + } + let name = subcommand.get_name(); + outpath.pop(); + outpath.push(format!("laze-{}.1", name)); + create_manpage(subcommand.clone(), &outpath)?; + } + + return Ok(0); } - return Ok(0); + _ => (), } if let Some(dir) = matches.get_one::("chdir") {