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.metakey Click Extra writes.Reserved for entries the framework owns. Downstream consumers picking their own
ctx.metakeys 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
argvslice fed to the current command.Written by
click_extra.commands.Command.make_contextso thatclick_extra.show_params.ShowParamsOptioncan re-parse the original arguments for the--paramstable without re-running the callbacks. Consumers normalize the parser’sUNSETsentinel back toNoneon read, matching whatclick.Command.parse_argsdoes forctx.params.Todo
Open a narrowly-scoped Click issue and PR for a public
click.Contextaccessor 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--paramstable and the configuration dump. Theclick-extra wrap --paramspath 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.mdunder “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_contexton the root context only (ctx.metais shared down the whole hierarchy, so a subcommand must not overwrite it). Equals Click’s rootinfo_name: the entry-point script name, the symlink name inmulticallpersonality mode, or an explicitprog_nameoverride passed tomain(). Read it withclick.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_confafter a configuration file is found and parsed.Noneif 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 byclick_extra.commands.Group(for subcommand inheritance) and byclick_extra.cli_wrapper.invoke_target()(to forward the loaded config to wrapped CLIs).With
cascade=Truethis 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_confas 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 whencascade=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 aconfig_schemais set. Read viaclick_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 ofctx.command_pathstrings. Thismetadict 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_subcommandstwice.
- click_extra.context.VERBOSITY_LEVEL: Final[str] = 'click_extra.verbosity_level'¶
The reconciled
LogLevelchosen for the run.Written by
_VerbosityOption.apply_verbosity, which reconciles every verbosity-related option (--verbosity,--verbose/-vand--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 LEVELas the user passed it.Written by
click_extra.logging.VerbosityOption.set_level(). Stored alongsideVERBOSITY_LEVELso downstream code can tell whether the final level came from--verbosityor from-v/-qrepetitions.
- click_extra.context.DEBUG: Final[str] = 'click_extra.debug'¶
Whether
--debugwas passed.Written by the
set_levelcallback ofclick_extra.logging.DebugOption. Read by_VerbosityOption.resolve_level, which answers it before anything else:--debugnames 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_levelcallback ofclick_extra.logging.VerboseOption. Combined withQUIETinto the signedverbose - quietcounter that_VerbosityOption.resolve_levelshifts the base level by.
- click_extra.context.QUIET: Final[str] = 'click_extra.quiet'¶
Raw repetition count of
--quiet/-q.Written by the
set_levelcallback ofclick_extra.logging.QuietOption. The quiet counterpart ofVERBOSE: each-qsubtracts one step from theverbose - quietnet 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--timeis 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
TableFormatchosen via--table-format.Written by
click_extra.table_options.TableFormatOption.init_formatter. Read byrender_table()andprint_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-byselection.Written by
click_extra.table_options.SortByOption.init_sortwhen its column definitions are known at declaration time (labeled mode). Read byrender_table()andprint_table()as their defaultsort_key. Absent in field-vocabulary mode, where the sort is resolved per table byclick_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 (likeclick_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
HelpThemeactive for this invocation.Written by
click_extra.theme.ThemeOption.set_theme. Read viaclick_extra.theme.get_current_theme(), which falls back toclick_extra.theme.default_themewhen 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.ConfigOptionwhen it sees[tool.<cli>.themes.<name>]tables: each table is built into aHelpTheme(cascading on top of an existing theme when name matches one already intheme_registry). Read byclick_extra.theme.get_theme_registry()so--themecan pick the new themes without leaking them into sibling invocations sharing the same process.
- click_extra.context.TELEMETRY: Final[str] = 'click_extra.telemetry'¶
Trueif the user opted into telemetry,Falseotherwise.Written by
click_extra.telemetry.TelemetryOption.set_telemetryafter reconciling--telemetry/--no-telemetrywith the standardDO_NOT_TRACKenvironment variable. Downstream code reads this to decide whether to emit usage data.
- click_extra.context.PROGRESS: Final[str] = 'click_extra.progress'¶
Truewhen the CLI may display progress spinners,Falseotherwise.Written by
click_extra.spinner.ProgressOption.set_progressfrom the--progress/--no-progressflag (which--accessiblelowers toFalse).Spinner,OperationTrailandprogressbar()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. SeeProgressOptionfor the rationale.
- click_extra.context.ACCESSIBLE: Final[str] = 'click_extra.accessible'¶
Truewhen the user requested screen-reader-friendly output.Written by
set_accessible()after reconciling the--accessibleflag with theACCESSIBLEenvironment variable. Read by output helpers that must degrade a cursor-driven element to a linear stream:clear()becomes a no-op andecho_via_pager()writes its text straight to stdout instead of spawning a pager.This is the readable counterpart to the
--color/--progress/--table-formatdefaults that--accessiblealso 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'¶
Truewhen 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 return0as long as the run itself succeeded.
- click_extra.context.pass_context(func)[source]¶
Mark a callback as wanting the active
Contextas its first argument.Click’s own
click.pass_context()is typed for the baseclick.Context. A handler annotated with click-extra’s enhancedContext(to reach its extra helpers likectx.print_table) therefore fails static type checking: function parameters are contravariant, soCallable[[Context], R]is not assignable where aCallable[[click.Context], R]is expected.This drop-in is typed for the enhanced
Contextand still accepts handlers typed for the baseclick.Context(a wider first parameter is allowed), so both type-check. At runtime it forwards the active context unchanged, exactly likeclick.pass_context().- Return type:
Callable[P,R]
- click_extra.context.get(ctx, key, default=None)[source]¶
Read
keyfrom the current context’s sharedmetadict.Equivalent to
ctx.meta.get(key, default). Click’smetais 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: