click_extra.config.export module¶

        classDiagram
  ExtraOption <|-- ExportConfigOption
    

Export the resolved configuration of a CLI as a configuration file.

Holds ExportConfigOption, the --export-config FORMAT option, and ensure_config_loaded(), which loads the configuration ahead of the introspection flags that read it.

click_extra.config.export.ensure_config_loaded(ctx)[source]¶

Run the sibling ConfigOption’s resolution if it has not run yet.

Click processes eager parameters given on the command line before eager parameters left at their defaults, so an explicitly-passed introspection flag (--params, --export-config) fires before the --config option had a chance to discover and load the configuration file. The views those flags render would then miss the configuration layer entirely, showing defaults where the user’s config applies.

Idempotent: does nothing when the config option already resolved (its callback stamps CONF_SOURCE on the context even when no file was found), when the command has no config option, or when the option carries no callback.

Return type:

None

class click_extra.config.export.ExportConfigOption(param_decls=None, *, type=None, metavar='FORMAT', is_eager=True, expose_value=False, help='Export the configuration in the selected format to <stdout>, then exit.', **kwargs)[source]¶

Bases: ExtraOption

A pre-configured option adding --export-config FORMAT.

Resolves the CLI’s current parameter values following Click’s precedence chain (command line, then environment variables, then configuration file, then defaults), renders them as a configuration file in the requested format on <stdout>, and exits.

Hint

Combine the flag with other options or environment variables to capture them in the generated configuration. For example, mycli --verbosity DEBUG --export-config toml emits a configuration whose verbosity is already set to DEBUG.

Like ValidateConfigOption, it relies on a sibling ConfigOption to provide the parameter structure and the excluded_params / included_params filter, so the export contains exactly the parameters that can be loaded back from a configuration file.

Note

The accepted formats are those serialize_content() can write (SERIALIZABLE_FORMATS). INI, SQLITE, ARGFILE and PYPROJECT_TOML have no serializer and cannot be dumped.

build_config(ctx, config_option)[source]¶

Resolve every config-eligible parameter into a dumpable tree.

Walks the sibling ConfigOption’s parameter structure, resolves each parameter’s effective value by replaying RAW_ARGS (falling back to defaults when the command did not capture them), drops the parameters is_excluded() rejects, and layers the coerced values into the {cli-name: {param: value, ...}} shape a configuration file uses. Parameter keys are rendered in their kebab-case spelling, the canonical presentation for configuration files; either spelling loads back to the same parameter.

Parameters without a default are kept as None leaves so the export names every key a configuration file can set: serializers render them as null, except TOML which comments them out (see _serialize_toml_with_unset). Loading null back is harmless: _install_default_map() cleans blank values out of the merged result.

Return type:

dict[str, Any]

export_config(ctx, param, value)[source]¶

Render the resolved configuration to <stdout> and exit.

Return type:

None