diff --git a/Cargo.lock b/Cargo.lock index 144d572..8d556da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -406,6 +406,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831" +[[package]] +name = "clap_mangen" +version = "0.2.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ea63a92086df93893164221ad4f24142086d535b3a0957b9b9bea2dc86301" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "colorchoice" version = "1.0.4" @@ -1543,6 +1553,7 @@ dependencies = [ "chrono", "clap", "clap_complete", + "clap_mangen", "console", "directories", "dockworker", @@ -1760,6 +1771,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "roff" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88f8660c1ff60292143c98d08fc6e2f654d722db50410e3f3797d40baaf9d8f3" + [[package]] name = "rust_decimal" version = "1.40.0" diff --git a/Cargo.toml b/Cargo.toml index 7e61552..e61010a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ anyhow = "1.0" byte-unit = "5.2" clap = {version = "4.4", features = ["derive"]} clap_complete = "4.4" +clap_mangen = "0.2" console = "0.16.2" chrono = {version = "0.4.44", default-features = false, features = ["clock"]} directories = "6.0.0" @@ -57,6 +58,7 @@ unicode-width = "0.2" anyhow = "1.0" clap = {version = "4.4", features = ["derive"]} clap_complete = "4.4" +clap_mangen = "0.2" [target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] pager = "0.16.1" diff --git a/build.rs b/build.rs index 8496ad5..d6be255 100644 --- a/build.rs +++ b/build.rs @@ -12,4 +12,19 @@ fn main() { gen_completion(Shell::PowerShell, &out_dir, &mut Opt::command()).unwrap(); gen_completion(Shell::Zsh, &out_dir, &mut Opt::command()).unwrap(); } + + // MAN_PATH=./man cargo build + if let Ok(path) = std::env::var("MAN_PATH") { + let out_dir = std::path::Path::new(&path); + std::fs::create_dir_all(out_dir).unwrap(); + let cmd = Opt::command(); + let man = clap_mangen::Man::new(cmd); + let mut buf = Vec::new(); + man.render(&mut buf).unwrap(); + std::fs::write(out_dir.join("procs.1"), buf).unwrap(); + println!( + "man page is generated: {}", + out_dir.join("procs.1").display() + ); + } } diff --git a/src/main.rs b/src/main.rs index 077d0aa..6ea5afe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ use crate::view::View; use crate::watcher::Watcher; use anyhow::{Context, Error}; use clap::{CommandFactory, Parser}; +use clap_mangen::Man; use console::Term; use once_cell::sync::Lazy; use std::cmp; @@ -155,6 +156,11 @@ fn run() -> Result<(), Error> { &mut stdout(), ); Ok(()) + } else if opt.gen_man_page { + let cmd = command_with_kind_values(); + let man = Man::new(cmd); + man.render(&mut stdout())?; + Ok(()) } else { let config = get_config(&opt)?; if opt.watch_mode { diff --git a/src/opt.rs b/src/opt.rs index 607b22d..c7e37dd 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -172,6 +172,10 @@ pub struct Opt { #[clap(long = "gen-completion-out", value_name = "shell")] pub gen_completion_out: Option, + /// Generate man page and write to stdout + #[clap(long = "gen-man-page")] + pub gen_man_page: bool, + /// Suppress header #[clap(long = "no-header")] pub no_header: bool,