| 1 |
""" |
| 2 |
click-man - Generate man pages for click application |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
This module implements the core functionality to |
| 6 |
generate man pages for entire click applications. |
| 7 |
|
| 8 |
:copyright: (c) 2016 by Timo Furrer. |
| 9 |
:license: MIT, see LICENSE for more details. |
| 10 |
""" |
| 11 |
|
| 12 |
import os |
| 13 |
from inspect import cleandoc |
| 14 |
|
| 15 |
import click |
| 16 |
|
| 17 |
from .man import ManPage |
| 18 |
|
| 19 |
CLICK_VERSION = tuple(int(x) for x in click.__version__.split('.')[:2]) |
| 20 |
|
| 21 |
|
| 22 |
def get_short_help_str(command, limit=45): |
| 23 |
""" |
| 24 |
Gets short help for the command or makes it by shortening the long help string. |
| 25 |
""" |
| 26 |
return ( |
| 27 |
command.short_help |
| 28 |
or command.help |
| 29 |
and click.utils.make_default_short_help(command.help, limit) |
| 30 |
or '' |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
def generate_man_page(ctx, version=None, date=None): |
| 35 |
""" |
| 36 |
Generate documentation for the given command. |
| 37 |
|
| 38 |
:param click.Context ctx: the click context for the |
| 39 |
cli application. |
| 40 |
:param str version: The version information to include in the man page. |
| 41 |
:param str date: The date information to include in the man page. |
| 42 |
|
| 43 |
:returns: the generate man page from the given click Context. |
| 44 |
:rtype: str |
| 45 |
""" |
| 46 |
|
| 47 |
man_page = ManPage(ctx.command_path) |
| 48 |
man_page.version = version |
| 49 |
man_page.short_help = get_short_help_str(ctx.command) |
| 50 |
man_page.description = cleandoc(ctx.command.help) |
| 51 |
man_page.synopsis = ' '.join(ctx.command.collect_usage_pieces(ctx)) |
| 52 |
man_page.options = [ |
| 53 |
x.get_help_record(ctx) |
| 54 |
for x in ctx.command.params |
| 55 |
if isinstance(x, click.Option) and not getattr(x, 'hidden', False) |
| 56 |
] |
| 57 |
|
| 58 |
if date: |
| 59 |
man_page.date = date |
| 60 |
|
| 61 |
commands = getattr(ctx.command, 'commands', None) |
| 62 |
if commands: |
| 63 |
man_page.commands = [ |
| 64 |
(k, get_short_help_str(v)) for k, v in commands.items() |
| 65 |
] |
| 66 |
|
| 67 |
return str(man_page) |
| 68 |
|
| 69 |
|
| 70 |
def write_man_pages( |
| 71 |
name, |
| 72 |
cli, |
| 73 |
parent_ctx=None, |
| 74 |
version=None, |
| 75 |
target_dir=None, |
| 76 |
date=None, |
| 77 |
): |
| 78 |
""" |
| 79 |
Generate man page files recursively |
| 80 |
for the given click cli function. |
| 81 |
|
| 82 |
:param str name: the cli name |
| 83 |
:param cli: the cli instance |
| 84 |
:param click.Context parent_ctx: the parent click context |
| 85 |
:param str target_dir: the directory where the generated |
| 86 |
man pages are stored. |
| 87 |
:param date: the date to include in the header |
| 88 |
""" |
| 89 |
ctx = click.Context(cli, info_name=name, parent=parent_ctx) |
| 90 |
|
| 91 |
man_page = generate_man_page(ctx, version) |
| 92 |
path = '{0}.1'.format(ctx.command_path.replace(' ', '-')) |
| 93 |
if target_dir: |
| 94 |
path = os.path.join(target_dir, path) |
| 95 |
|
| 96 |
with open(path, 'w+') as f: |
| 97 |
f.write(man_page) |
| 98 |
|
| 99 |
commands = getattr(cli, 'commands', {}) |
| 100 |
for name, command in commands.items(): |
| 101 |
if CLICK_VERSION >= (7, 0): |
| 102 |
|
| 103 |
if command.hidden: |
| 104 |
|
| 105 |
continue |
| 106 |
|
| 107 |
write_man_pages( |
| 108 |
name, |
| 109 |
command, |
| 110 |
parent_ctx=ctx, |
| 111 |
version=version, |
| 112 |
target_dir=target_dir, |
| 113 |
date=date, |
| 114 |
) |