diff --git a/scripts/generate_man.py b/scripts/generate_man.py index 93adb33ee0..b8d09e46a0 100644 --- a/scripts/generate_man.py +++ b/scripts/generate_man.py @@ -7,20 +7,43 @@ from build_manpages.manpage import Manpage # type: ignore from pipx.main import get_command_parser +from typing import NoReturn +import os +from build_manpages.manpage import Manpage -def main(): - parser = get_command_parser() - parser.man_short_description = parser.description.splitlines()[1] +def get_manpage_body(parser) -> str: + """ + Generate the body of the man page for the pipx command line tool. + + Args: + parser: The command parser. + + Returns: + The body of the man page as a string. + """ + parser.man_short_description = parser.description.splitlines()[1] manpage = Manpage(parser) body = str(manpage) # Avoid hardcoding build paths in manpages (and improve readability) body = body.replace(os.path.expanduser("~").replace("-", "\\-"), "~") - # Add a credit section - body += textwrap.dedent( + return body + + +def add_credit_section(body: str) -> str: + """ + Add a credit section to the body of the man page. + + Args: + body: The body of the man page. + + Returns: + The body of the man page with the credit section added. + """ + credit_section = textwrap.dedent( """ .SH AUTHORS .IR pipx (1) @@ -34,9 +57,37 @@ def main(): """ ) + return body + credit_section + + +def write_manpage(body: str) -> NoReturn: + """ + Write the man page to a file named `pipx.1`. + + Args: + body: The body of the man page. + """ with open("pipx.1", "w") as f: f.write(body) +def main() -> NoReturn: + """Generate and write the man page for pipx command line tool. + + This function generates the man page for the pipx command line tool using the `Manpage` class from `build_manpages` module. + It adds a credit section and writes the generated man page to a file named `pipx.1`. + + Raises: + FileNotFoundError: If the file `pipx.1` cannot be found. + + Examples: + >>> main() + """ + parser = get_command_parser() + body = get_manpage_body(parser) + body_with_credit = add_credit_section(body) + write_manpage(body_with_credit) + + if __name__ == "__main__": main()