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_labelhelper; 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:
objectStructured collection of keywords extracted from a Click context for help screen highlighting.
Each field corresponds to a semantic category with its own styling.
- deprecated: set[str]¶
Markers a CLI paints with the
deprecatedslot beside Click’s own.DEPRECATED_REcovers 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.
- class click_extra.highlight.HelpFormatter(*args, **kwargs)[source]¶
Bases:
HelpFormatterExtends 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 ofThemeOption, or the process-wide default when no context is active. A plaincloup.HelpThemelends the slots it declares to that active theme, which keeps the slots cloup lacks.- keywords: HelpKeywords¶
Keywords to highlight, collected from the rendered command’s context.
Instance state, initialized per formatter:
_HelpColorsMixin.format_helpfills it before rendering, andhighlight_extra_keywords()mutates it (see theexcluded_keywordssubtraction), 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:
- 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 theinvoked_commandslot. A subcommand’s path ends on a name the parent screen lists undersubcommand, and the same word in two colors across two screens reads as two different things.- Return type:
- 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 requiredclick.Choiceoperand),[json|xml|csv](an optional one) or[id,spec,value](Click Extra’s multi-pickMultiChoice), and paints every known value withtheme.choice. A part that is not one is a type placeholder, like theINTEGERof a hybrid[auto|max|INTEGER], and takestheme.metavar.Shared by the help screen and by
render_command_tree(), so one operand reads the same wherever it is drawn.
- click_extra.highlight.highlight(content, patterns, styling_func, ignore_case=False)[source]¶
Highlights parts of the
contentthat matchespatterns.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Ăźestring matching theStrassecontent.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: