-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclick_format.py
More file actions
71 lines (55 loc) · 2.53 KB
/
click_format.py
File metadata and controls
71 lines (55 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Released under MIT License.
# Copyright (c) 2025-2026 Ladislav Bartos and Robert Vacha Lab
"""
GNU-style help formatting for Click commands.
This module defines `GNUHelpColorsCommand`, a Click command class that prints
help text using GNU-style formatting with customizable colors, headings, and
option layouts.
"""
from collections.abc import Sequence
import click
from click import Context, HelpFormatter
from click_help_colors import HelpColorsCommand
class GNUHelpColorsCommand(HelpColorsCommand):
"""Custom formatter that prints options in GNU-style."""
def get_help(self, ctx: Context) -> str:
class GNUHelpFormatter(HelpFormatter):
def __init__(self, width=None, headers_color=None, options_color=None):
super().__init__(width=width)
self.headers_color = headers_color or "white"
self.options_color = options_color or "white"
def write_heading(self, heading: str) -> None:
styled_heading = click.style(heading, fg=self.headers_color, bold=True)
self.write(f"{styled_heading}\n")
def write_usage(
self, prog_name: str, args: str | None, prefix: str | None = None
) -> None:
"""Override to make Usage: header bold"""
if prefix is None:
prefix = "Usage:"
styled_prefix = click.style(prefix, fg=self.headers_color, bold=True)
usage_line = f"{styled_prefix} {prog_name}"
if args:
usage_line += f" {args}"
self.write(f"{usage_line}\n")
def write_dl(
self,
rows: Sequence[tuple[str, str | None]],
_col_max: int = 30,
_col_spacing: int = 2,
) -> None:
for term, definition in rows:
colored_term = click.style(term, fg=self.options_color, bold=True)
self.write(f" {colored_term}\n")
if definition:
for line in definition.splitlines():
if line.strip():
self.write(f" {line}\n")
self.write("\n")
formatter = GNUHelpFormatter(
width=ctx.terminal_width,
headers_color=getattr(self, "help_headers_color", "white"),
options_color=getattr(self, "help_options_color", "white"),
)
self.format_help(ctx, formatter)
return formatter.getvalue()