click_extra.highlight module¶

        classDiagram
  HelpFormatter <|-- HelpFormatter
    

Help-screen keyword highlighting and the colorized help formatter.

Hosts the engine that collects highlightable keywords from a Click context (HelpKeywords, _HelpColorsMixin) and renders them with the active theme: HelpFormatter styles --help output and highlight() applies a styling function to arbitrary matches.

Note

The patterns of the help screen stay uncompiled strings at module level. Most runs of a CLI render no help screen, so none of them is worth compiling on import: re compiles each on its first use and caches it from then on. DEPRECATED_RE is the exception: highlight() reads a string as a literal, so the marker pattern other renderers share is handed over compiled.

click_extra.highlight.DEPRECATED_RE = re.compile('\\(deprecated(?::\\s[^)]+)?\\)', re.IGNORECASE)¶

Matches (DEPRECATED) and (DEPRECATED: reason) markers, whatever their case.

The canonical spelling comes from Click’s shared _format_deprecated_label helper; the case-insensitive flag also catches the variants a CLI author writes by hand in a help string.

Read by the help screen and by render_command_tree(), so one marker is painted the same wherever a command’s description is drawn.

class click_extra.highlight.HelpKeywords(cli_names=set(), subcommands=set(), command_aliases=set(), arguments=set(), long_options=set(), short_options=set(), choices=set(), choice_metavars=set(), metavars=set(), envvars=set(), defaults=set(), deprecated=set())[source]¶

Bases: object

Structured collection of keywords extracted from a Click context for help screen highlighting.

Each field corresponds to a semantic category with its own styling.

cli_names: set[str]¶
subcommands: set[str]¶
command_aliases: set[str]¶
arguments: set[str]¶
long_options: set[str]¶
short_options: set[str]¶
choices: set[str]¶
choice_metavars: set[str]¶
metavars: set[str]¶
envvars: set[str]¶
defaults: set[str]¶
deprecated: set[str]¶

Markers a CLI paints with the deprecated slot beside Click’s own.

DEPRECATED_RE covers the spellings Click writes, and only those. A project marking a parameter in its own vocabulary ((unmaintained) for a tool whose upstream is gone, where the option itself is not being retired) otherwise gets no color for it, since the word is the whole pattern.

Each entry is matched literally, so a marker is written the way it reads on the screen, punctuation included.

merge(other)[source]¶

Merge another HelpKeywords into this one.

Each set field is updated with the corresponding set from other.

Return type:

None

subtract(other)[source]¶

Remove keywords found in other from this instance.

Each set field is difference-updated with the corresponding set from other. Mirror of merge().

Return type:

None

class click_extra.highlight.HelpFormatter(*args, **kwargs)[source]¶

Bases: HelpFormatter

Extends Cloup’s custom HelpFormatter to highlight options, choices, metavars and default values.

Cloup declined this regex-based highlighting, at janluke/cloup#95 and janluke/cloup#97. The same proposal is open at click-contrib/click-help-colors#17.

Force the theme to a HelpTheme.

The theme defaults to the one active for the current invocation, as click_extra.theme.get_current_theme() resolves it: the per-invocation pick of ThemeOption, or the process-wide default when no context is active. A plain cloup.HelpTheme lends the slots it declares to that active theme, which keeps the slots cloup lacks.

theme: HelpTheme¶
keywords: HelpKeywords¶

Keywords to highlight, collected from the rendered command’s context.

Instance state, initialized per formatter: _HelpColorsMixin.format_help fills it before rendering, and highlight_extra_keywords() mutates it (see the excluded_keywords subtraction), so a shared class-level default would leak keywords across formatters.

excluded_keywords: HelpKeywords | None¶

Keywords subtracted from the cross-reference passes, or None.

highlight_extra_keywords(help_text)[source]¶

Highlight extra keywords in help screens based on the theme.

Uses the highlight() function for all keyword categories. Each category is processed as a batch of regex patterns with a single styling function, which handles overlapping matches and prevents double-styling.

Return type:

str

write_usage(prog, args='', prefix=None)[source]¶

Draw the usage line, naming the command the same way the rest does.

Reimplements cloup.HelpFormatter.write_usage, which paints the whole command path with the invoked_command slot. A subcommand’s path ends on a name the parent screen lists under subcommand, and the same word in two colors across two screens reads as two different things.

Return type:

None

getvalue()[source]¶

Wrap original Click.HelpFormatter.getvalue() to force extra-colorization on rendering.

Return type:

str

click_extra.highlight.style_choice_metavar(metavar, choices, theme)[source]¶

Style each part of a metavar that enumerates the values it accepts.

Takes a rendered metavar like {json|xml|csv} (a required click.Choice operand), [json|xml|csv] (an optional one) or [id,spec,value] (Click Extra’s multi-pick MultiChoice), and paints every known value with theme.choice. A part that is not one is a type placeholder, like the INTEGER of a hybrid [auto|max|INTEGER], and takes theme.metavar.

Shared by the help screen and by render_command_tree(), so one operand reads the same wherever it is drawn.

Parameters:
  • metavar (str) – the rendered metavar to style.

  • choices (Collection[str]) – the values the parameter accepts.

  • theme (HelpTheme) – the theme whose choice and metavar slots paint the parts.

Return type:

str | None

Returns:

the styled metavar, or None when it enumerates nothing.

click_extra.highlight.highlight(content, patterns, styling_func, ignore_case=False)[source]¶

Highlights parts of the content that matches patterns.

Takes care of overlapping parts within the content, so that the styling function is applied only once to each contiguous range of matching characters.

Todo

Support case-folding, so we can have the StraĂźe string matching the Strasse content.

This could be tricky as it messes with string length and characters index, which our logic relies on.

Danger

Roundtrip through lower-casing/upper-casing is a can of worms, because some characters change length when their case is changed:

Return type:

str