Skip to main content

rusmes_cli/commands/
man.rs

1//! Man page generation command
2
3use anyhow::Result;
4use clap::CommandFactory;
5use clap_mangen::Man;
6use std::io::Write;
7
8/// Write roff-formatted man page for the given CLI application to stdout.
9///
10/// The output can be piped directly into `man -l -` for immediate viewing, or
11/// stored in a system man directory (e.g. `/usr/local/share/man/man1/`).
12///
13/// # Errors
14/// Returns an error if writing to stdout fails.
15pub fn run<A>() -> Result<()>
16where
17    A: CommandFactory,
18{
19    let cmd = A::command();
20    let man = Man::new(cmd);
21    let mut stdout = std::io::stdout();
22    man.render(&mut stdout)?;
23    stdout.flush()?;
24    Ok(())
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    /// Verify man page output contains the `.TH` roff macro, which is the
32    /// mandatory title header for a well-formed man page.
33    #[test]
34    fn man_page_produces_valid_roff() {
35        use crate::CliApp;
36        use std::io::Write;
37
38        let cmd = CliApp::command();
39        let man = Man::new(cmd);
40        let mut buf: Vec<u8> = Vec::new();
41        man.render(&mut buf).expect("man page render");
42        buf.flush().ok();
43
44        let output = String::from_utf8(buf).expect("utf-8");
45        assert!(
46            output.contains(".TH"),
47            "man page output should contain the .TH roff macro — got:\n{output}"
48        );
49    }
50}