click_extra.context module

        classDiagram
  dict <|-- _LazyMetaDict
    

Click context plumbing: the central registry of every ctx.meta key Click Extra writes or reads.

The Context subclass lives in click_extra.commands, beside the commands that create it.

Click’s click.Context.meta is a per-invocation dict that Click shares across the parent/child context hierarchy. Click Extra uses it to pass per-invocation state (the picked theme, the resolved table format, the loaded configuration, etc.) between eager callbacks and the rest of the CLI without mutating module-level globals. Per-invocation context storage is what keeps back-to-back invocations of the same CLI (Sphinx builds, test runners, REPLs) from leaking state into each other.

This module is part of Click Extra’s public API. Inside any @command- or @group-decorated function, request the active context with click.pass_context() (or call click.get_current_context()) and read the entries you need:

from click_extra import command, context, echo, pass_context


@command
@pass_context
def cli(ctx):
    echo(f"Theme: {ctx.meta[context.THEME]}")
    echo(f"Jobs:  {ctx.meta[context.JOBS]}")

Each constant below documents who writes the entry, when, and what shape the value takes. The raw string values are stable and downstream code may also read ctx.meta["click_extra.<field>"] directly: the constants exist so internal call sites and downstream code can converge on a single spelling.

click_extra.context.META_NAMESPACE: Final[str] = 'click_extra.'

Prefix shared by every ctx.meta key Click Extra writes.

Reserved for entries the framework owns. Downstream consumers picking their own ctx.meta keys are encouraged to use a different prefix to avoid colliding with current or future Click Extra entries.

click_extra.context.RAW_ARGS: Final[str] = 'click_extra.raw_args'

The raw, pre-parsed argv slice fed to the current command.

Written by click_extra.commands.Command.make_context so that click_extra.show_params.ShowParamsOption can re-parse the original arguments for the --params table without re-running the callbacks. Consumers normalize the parser’s UNSET sentinel back to None on read, matching what click.Command.parse_args does for ctx.params.

Todo

Open a narrowly-scoped Click issue and PR for a public click.Context accessor returning a parameter’s resolved (value, ParameterSource) after parsing, without re-firing eager callbacks. That is the forward resolution a live CLI needs: landing it retires the replay behind the --params table and the configuration dump. The click-extra wrap --params path keeps this constant and its replay, because Click never parses the foreign CLI it renders.

Reference pallets/click#1279 as related, not as home: it asks for the inverse direction (reconstructing a normalized argv from a Context), was scoped with about a dozen normalization rules, flagged underdefined, and has stalled since 2023. Tracked in docs/upstream.md under “Normalized arguments”; the developer note below records why the cheaper alternatives do not work.

click_extra.context.INVOCATION_NAME: Final[str] = 'click_extra.invocation_name'

The name the CLI was invoked under, as resolved on the root context.

Written by click_extra.commands.Command.make_context on the root context only (ctx.meta is shared down the whole hierarchy, so a subcommand must not overwrite it). Equals Click’s root info_name: the entry-point script name, the symlink name in multicall personality mode, or an explicit prog_name override passed to main(). Read it with click.pass_context() to implement invocation-name-aware behavior without the full multicall machinery:

if ctx.meta[context.INVOCATION_NAME] == "bunzip2":
    ...
click_extra.context.CONF_SOURCE: Final[str] = 'click_extra.conf_source'

Resolved path or URL of the configuration file that was loaded.

Written by click_extra.config.option.ConfigOption.load_conf after a configuration file is found and parsed. None if no file matched.

click_extra.context.CONF_FULL: Final[str] = 'click_extra.conf_full'

Full parsed configuration document (the whole file, every section).

Written by click_extra.config.option.ConfigOption.load_conf. Read by click_extra.commands.Group (for subcommand inheritance) and by click_extra.cli_wrapper.invoke_target() (to forward the loaded config to wrapped CLIs).

With cascade=True this holds the deep-merged view of every loaded file, which is the configuration as it was actually applied.

click_extra.context.CONF_SOURCES: Final[str] = 'click_extra.conf_sources'

Every configuration file loaded, in precedence order (highest first).

Written by click_extra.config.option.ConfigOption.load_conf as a (possibly empty) tuple of (location, parsed_conf) pairs. Holds a single entry in the default first-file-wins mode, and one entry per loaded file when cascade=True.

click_extra.context.TOOL_CONFIG: Final[str] = 'click_extra.tool_config'

The app-specific config section, deserialized through config_schema.

Written by load_conf() when a config_schema is set. Read via click_extra.config.schema.get_tool_config().

click_extra.context.SUBCOMMANDS_APPLIED: Final[str] = 'click_extra.subcommands_applied'

Command paths whose reserved subcommand keys were already spliced in.

Written by click_extra.config.subcommands.inject_reserved_subcommands() as a growing set of ctx.command_path strings. This meta dict is shared by the whole context tree, so the set is what tells one group’s injection from another’s, and what keeps a second pass over the same group from prepending its _prepend_subcommands twice.

click_extra.context.VERBOSITY_LEVEL: Final[str] = 'click_extra.verbosity_level'

The reconciled LogLevel chosen for the run.

Written by _VerbosityOption.apply_verbosity, which reconciles every verbosity-related option (--verbosity, --verbose/-v and --quiet/-q) into a single level. Read by the same method to detect whether the reconciled level changed since a sibling option last fired.

click_extra.context.VERBOSITY: Final[str] = 'click_extra.verbosity'

Raw value of --verbosity LEVEL as the user passed it.

Written by click_extra.logging.VerbosityOption.set_level(). Stored alongside VERBOSITY_LEVEL so downstream code can tell whether the final level came from --verbosity or from -v/-q repetitions.

click_extra.context.DEBUG: Final[str] = 'click_extra.debug'

Whether --debug was passed.

Written by the set_level callback of click_extra.logging.DebugOption. Read by _VerbosityOption.resolve_level, which answers it before anything else: --debug names the loudest level there is, so nothing another option asks for can outrank it.

click_extra.context.VERBOSE: Final[str] = 'click_extra.verbose'

Raw repetition count of --verbose/-v.

Written by the set_level callback of click_extra.logging.VerboseOption. Combined with QUIET into the signed verbose - quiet counter that _VerbosityOption.resolve_level shifts the base level by.

click_extra.context.QUIET: Final[str] = 'click_extra.quiet'

Raw repetition count of --quiet/-q.

Written by the set_level callback of click_extra.logging.QuietOption. The quiet counterpart of VERBOSE: each -q subtracts one step from the verbose - quiet net applied on top of the base verbosity level.

click_extra.context.START_TIME: Final[str] = 'click_extra.start_time'

time.perf_counter() snapshot taken when --time is enabled.

Written by click_extra.execution.TimerOption.init_timer.

click_extra.context.JOBS: Final[str] = 'click_extra.jobs'

Effective parallel job count after clamping (always >= 1).

Written by click_extra.jobs.JobsOption.validate_jobs. Click Extra itself does not act on this value: it is a contract for downstream commands that drive their own concurrency.

click_extra.context.TABLE_FORMAT: Final[str] = 'click_extra.table_format'

The TableFormat chosen via --table-format.

Written by click_extra.table_options.TableFormatOption.init_formatter. Read by render_table() and print_table() as their default format.

click_extra.context.SORT_BY: Final[str] = 'click_extra.sort_by'

Tuple of column IDs picked via --sort-by (in priority order).

Written by click_extra.table_options.SortByOption.init_sort.

click_extra.context.TABLE_SORT_KEY: Final[str] = 'click_extra.table_sort_key'

Row sort key derived from the --sort-by selection.

Written by click_extra.table_options.SortByOption.init_sort when its column definitions are known at declaration time (labeled mode). Read by render_table() and print_table() as their default sort_key. Absent in field-vocabulary mode, where the sort is resolved per table by click_extra.table.print_table() from the column IDs its headers carry.

click_extra.context.COLUMNS: Final[str] = 'click_extra.columns'

Tuple of column IDs selected via --columns (in display order).

Written by click_extra.table_options.ColumnsOption.init_columns. Read by table-rendering consumers (like click_extra.show_params.ShowParamsOption) to project and reorder columns before emitting the table. Empty / unset means no projection: render every column in its canonical order.

click_extra.context.THEME: Final[str] = 'click_extra.theme.active'

The HelpTheme active for this invocation.

Written by click_extra.theme.ThemeOption.set_theme. Read via click_extra.theme.get_current_theme(), which falls back to click_extra.theme.default_theme when no key is set.

click_extra.context.THEME_OVERRIDES: Final[str] = 'click_extra.theme.overrides'

Per-invocation theme registry overlay loaded from the user’s config file.

Written by click_extra.config.option.ConfigOption when it sees [tool.<cli>.themes.<name>] tables: each table is built into a HelpTheme (cascading on top of an existing theme when name matches one already in theme_registry). Read by click_extra.theme.get_theme_registry() so --theme can pick the new themes without leaking them into sibling invocations sharing the same process.

click_extra.context.TELEMETRY: Final[str] = 'click_extra.telemetry'

True if the user opted into telemetry, False otherwise.

Written by click_extra.telemetry.TelemetryOption.set_telemetry after reconciling --telemetry / --no-telemetry with the standard DO_NOT_TRACK environment variable. Downstream code reads this to decide whether to emit usage data.

click_extra.context.PROGRESS: Final[str] = 'click_extra.progress'

True when the CLI may display progress spinners, False otherwise.

Written by click_extra.spinner.ProgressOption.set_progress from the --progress / --no-progress flag (which --accessible lowers to False). Spinner, OperationTrail and progressbar() read it on their own when left at their automatic default.

Deliberately independent of color: a spinner is an interactivity concern, so it is gated on the terminal (TTY / TERM=dumb, handled by the spinner) and on explicit intent (--no-progress / --accessible), never on --no-color / NO_COLOR. See ProgressOption for the rationale.

click_extra.context.ACCESSIBLE: Final[str] = 'click_extra.accessible'

True when the user requested screen-reader-friendly output.

Written by set_accessible() after reconciling the --accessible flag with the ACCESSIBLE environment variable. Read by output helpers that must degrade a cursor-driven element to a linear stream: clear() becomes a no-op and echo_via_pager() writes its text straight to stdout instead of spawning a pager.

This is the readable counterpart to the --color / --progress / --table-format defaults that --accessible also lowers: those are consumed through their own resolved values (ctx.color, PROGRESS, the table format), while this flag exposes the accessibility intent itself.

click_extra.context.ZERO_EXIT: Final[str] = 'click_extra.zero_exit'

True when the user asked the CLI to always return a zero exit code.

Written by click_extra.execution.ZeroExitOption.set_zero_exit. Click Extra itself does not act on this value: it is a contract for downstream commands that suppress their non-zero “problems found” exit code and return 0 as long as the run itself succeeded.

click_extra.context.pass_context(func)[source]

Mark a callback as wanting the active Context as its first argument.

Click’s own click.pass_context() is typed for the base click.Context. A handler annotated with click-extra’s enhanced Context (to reach its extra helpers like ctx.print_table) therefore fails static type checking: function parameters are contravariant, so Callable[[Context], R] is not assignable where a Callable[[click.Context], R] is expected.

This drop-in is typed for the enhanced Context and still accepts handlers typed for the base click.Context (a wider first parameter is allowed), so both type-check. At runtime it forwards the active context unchanged, exactly like click.pass_context().

Return type:

Callable[P, R]

click_extra.context.get(ctx, key, default=None)[source]

Read key from the current context’s shared meta dict.

Equivalent to ctx.meta.get(key, default). Click’s meta is shared across the parent/child hierarchy, so reading from the local context is sufficient: there is no need to walk up to the root manually.

Return type:

Any

click_extra.context.set(ctx, key, value)[source]

Write value under key in the current context’s shared meta dict.

Equivalent to ctx.meta[key] = value. Provided as the symmetric writer for get() so that callers can route both sides of a meta access through this module.

Return type:

None

click_extra.context.setdefault(ctx, key, factory)[source]

Read key from the shared meta dict, first storing factory() if missing.

factory runs for a missing key only, so each invocation gets a mutable value of its own, like the set of command paths a run marks.

Return type:

Any