Skip to content

Commit 2e51c9d

Browse files
Generate manual page versions of documentation for help command
Manual pages are included in distributions so that they can be accessed reliably, but should be built separately if installing as a system package.
1 parent b01fdd7 commit 2e51c9d

File tree

7 files changed

+65
-8
lines changed

7 files changed

+65
-8
lines changed

.github/workflows/publish.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ jobs:
2121
uses: actions/setup-python@v2
2222
with:
2323
python-version: '3.10'
24+
sphinx: '5.3.0'
25+
26+
- name: Generate packaged manual pages
27+
run: sphinx-build -b man -W docs mons/man -d docs/_build
2428

2529
- name: Install pypa/build
2630
run: python -m pip install build --user

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Project-specific files
2+
mons/man/*
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
recursive-include mons/man/man[0-9] *

docs/conf.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
# This pattern also affects html_static_path and html_extra_path.
4040
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
4141

42+
# -- Options for manual page output ------------------------------------------
43+
44+
man_pages = [
45+
("everest", "mons", "", "", "1"),
46+
("mods", "mons-mods", "", "", "1"),
47+
]
48+
49+
man_make_section_directory = True
50+
manpages_url = "https://www.man7.org/linux/man-pages/man{section}/{page}.{section}.html"
51+
4252

4353
# -- Options for HTML output -------------------------------------------------
4454

mons/mons.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
#!/usr/bin/env python
22
import logging
33
import os
4+
import shutil
5+
import subprocess
6+
import sys
47
import typing as t
58
from importlib import import_module
69

10+
# namespace package support added in 3.10
11+
if sys.version_info >= (3, 10): # novermin
12+
from importlib.resources import files
13+
else:
14+
from importlib_resources import files
15+
716
import click
817

918
import mons.clickExt as clickExt
@@ -38,24 +47,45 @@ def cli(ctx: click.Context):
3847
ctx.parent = env_ctx
3948

4049

50+
_MAN_PAGES = {
51+
"": ("1", "mons"),
52+
"mons": ("1", "mons"),
53+
"mods": ("1", "mons-mods"),
54+
}
55+
56+
4157
@cli.command(context_settings={"ignore_unknown_options": True})
4258
@click.argument("command", nargs=-1)
4359
@click.pass_context
4460
def help(ctx: click.Context, command: t.List[str]):
4561
"""Display help text for a command."""
62+
try:
63+
man_pages = files("mons.man")
64+
except FileNotFoundError:
65+
man_pages = None
4666

47-
# No args means print program help
48-
if len(command) < 1 and ctx.parent:
49-
click.echo(cli.get_help(ctx.parent))
50-
exit(0)
67+
man_path = shutil.which("man")
68+
if man_pages and man_path and " ".join(command).lower() in _MAN_PAGES:
69+
cmd_str = " ".join(command).lower()
70+
section, page = _MAN_PAGES[cmd_str]
71+
subprocess.run(
72+
[
73+
man_path,
74+
"--local-file",
75+
"-",
76+
],
77+
env=env,
78+
input=man_pages.joinpath(f"man{section}", f"{page}.{section}").read_bytes(),
79+
)
80+
return
5181

5282
group = cli
5383
cmd_path = []
5484
for cmd_name in command:
5585
cmd_path.append(cmd_name)
5686
cmd = group.get_command(ctx, cmd_name)
5787
if not cmd:
58-
err_msg = "No such command '{}.'".format(" ".join(cmd_path))
88+
err_msg = "No help entry for '{}'.".format(" ".join(cmd_path))
5989
click.echo(str(click.BadArgumentUsage(err_msg, ctx)))
6090
exit(click.BadArgumentUsage.exit_code)
6191

setup.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ install_requires =
2424
pyyaml
2525
urllib3
2626
platformdirs
27-
package_dir =
28-
packages = find:
27+
typing_extensions
28+
importlib_resources;python_version<'3.10'
29+
packages = find_namespace:
30+
include_package_data = True
31+
32+
[options.packages.find]
33+
include = mons*
2934

3035
[options.entry_points]
3136
console_scripts =

tox.ini

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ commands =
2424
pyright --pythonplatform Windows --stats
2525

2626
[testenv:docs]
27-
commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html
27+
deps = -r requirements-dev.txt
28+
skip_install = true
29+
commands =
30+
sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html
31+
sphinx-build -W -b man -d {envtmpdir}/doctrees docs {envtmpdir}/man

0 commit comments

Comments
 (0)