From 868aa30ef5d2180713b78dad69dcbebd1892569a Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Sun, 28 Jan 2024 02:51:30 +0100 Subject: [PATCH 1/2] feat(mangen): Generate filename and files The API is inspired by clap_complete. --- clap_mangen/src/lib.rs | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/clap_mangen/src/lib.rs b/clap_mangen/src/lib.rs index 70b151a07f9..025a291624c 100644 --- a/clap_mangen/src/lib.rs +++ b/clap_mangen/src/lib.rs @@ -95,6 +95,51 @@ impl Man { } } +/// Handle [`Man`] in relation to files +impl Man { + /// Generate the filename of the manual page + #[must_use] + pub fn get_filename(&self) -> String { + format!( + "{}.{}", + self.cmd + .get_display_name() + .unwrap_or_else(|| self.cmd.get_name()), + self.section + ) + } + + /// [Renders](Man::render) the manual page and writes it to a file + pub fn generate_to(&self, out_dir: Dir) -> Result + where + Dir: AsRef, + { + let filepath = out_dir.as_ref().join(self.get_filename()); + let mut file = std::fs::File::create(&filepath)?; + self.render(&mut file)?; + file.flush()?; + Ok(filepath) + } +} + +/// Generate manual page files for the command with all subcommands +pub fn generate_to(cmd: clap::Command, out_dir: Dir) -> Result<(), std::io::Error> +where + Dir: AsRef, +{ + fn generate(cmd: clap::Command, out_dir: &std::path::Path) -> Result<(), std::io::Error> { + for cmd in cmd.get_subcommands().cloned() { + generate(cmd, out_dir)?; + } + Man::new(cmd).generate_to(out_dir)?; + Ok(()) + } + + let mut cmd = cmd.disable_help_subcommand(true); + cmd.build(); + generate(cmd, out_dir.as_ref()) +} + /// Generate ROFF output impl Man { /// Render a full manual page into the writer. From 2b9d991f51f1366defb21f9e2e6aa3f5bd817f5e Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Tue, 30 Jan 2024 22:22:46 +0100 Subject: [PATCH 2/2] refactor(mangen): Use argument impl over generic --- clap_mangen/src/lib.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clap_mangen/src/lib.rs b/clap_mangen/src/lib.rs index 025a291624c..192edcc3c9a 100644 --- a/clap_mangen/src/lib.rs +++ b/clap_mangen/src/lib.rs @@ -110,10 +110,10 @@ impl Man { } /// [Renders](Man::render) the manual page and writes it to a file - pub fn generate_to(&self, out_dir: Dir) -> Result - where - Dir: AsRef, - { + pub fn generate_to( + &self, + out_dir: impl AsRef, + ) -> Result { let filepath = out_dir.as_ref().join(self.get_filename()); let mut file = std::fs::File::create(&filepath)?; self.render(&mut file)?; @@ -123,10 +123,10 @@ impl Man { } /// Generate manual page files for the command with all subcommands -pub fn generate_to(cmd: clap::Command, out_dir: Dir) -> Result<(), std::io::Error> -where - Dir: AsRef, -{ +pub fn generate_to( + cmd: clap::Command, + out_dir: impl AsRef, +) -> Result<(), std::io::Error> { fn generate(cmd: clap::Command, out_dir: &std::path::Path) -> Result<(), std::io::Error> { for cmd in cmd.get_subcommands().cloned() { generate(cmd, out_dir)?;