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--configoption 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_SOURCEon the context even when no file was found), when the command has no config option, or when the option carries no callback.- Return type:
- 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:
ExtraOptionA 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 tomlemits a configuration whoseverbosityis already set toDEBUG.Like
ValidateConfigOption, it relies on a siblingConfigOptionto provide the parameter structure and theexcluded_params/included_paramsfilter, 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,ARGFILEandPYPROJECT_TOMLhave 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 replayingRAW_ARGS(falling back to defaults when the command did not capture them), drops the parametersis_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
Noneleaves so the export names every key a configuration file can set: serializers render them asnull, except TOML which comments them out (see_serialize_toml_with_unset). Loadingnullback is harmless:_install_default_map()cleans blank values out of the merged result.