mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
Merge pull request #5331 from EdJoPaTo/mangen-files
feat(mangen): generate filename and files
This commit is contained in:
commit
afcd8000e8
1 changed files with 45 additions and 0 deletions
|
@ -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: impl AsRef<std::path::Path>,
|
||||
) -> Result<std::path::PathBuf, std::io::Error> {
|
||||
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: impl AsRef<std::path::Path>,
|
||||
) -> 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)?;
|
||||
}
|
||||
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.
|
||||
|
|
Loading…
Reference in a new issue