Skip to content

Coderbotics: Refactoring of refactor_generate_man.py #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions scripts/generate_man.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()