Config file generation#44
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds config file generation to argclass — the inverse of the existing
config_files=reading. AConfigGeneratorwalks the parser tree (mirroringParser._fill_group), builds a nested dict of the current state, and rendersit. Symmetric with
AbstractDefaultsParserand its subclasses.Built-in generators
INIConfigGenerator;lines fromhelp=JSONConfigGeneratorTOMLConfigGenerator#lines fromhelp=EnvConfigGenerator.env#lines fromhelp=TOML emission is hand-rolled (~50 lines) so the zero-dependency guarantee stays intact. Covers
str/int/float/bool/list/None.Extension points
ConfigGenerator— base class. Subclass and overriderender(data, help_map)to add a new format.GenerateConfigAction— argparse Action that takes agenerator=passthrough kwarg. Wires--generate-config FILE(use-for stdout) in user CLIs. Reads CLI args from the argparse namespace and env vars fromos.environat dump time, so the output reflects the parser's current resolved state even when invoked mid-parse.NonConfigAction— opt-out marker for "fire and exit" actions (--version,--check-updates,GenerateConfigActionitself). Generators consult__emit_config__and skip such args from dumps.argparse's built-in
--help/--versionare recognised by enum value.Sources reflected in the dump
Whatever argclass would resolve at the moment of dumping ends up in the output — defaults,
config_files=, env vars (explicitenv_var=orauto_env_var_prefix=), and CLI args. This makes generation useful formore than scaffolding: format conversion (load INI, dump TOML), snapshotting a deployed config, or materialising env-based config to a file.
Demo
Public API additions
Plus the introspection helpers in
argclass.emit:should_emit,current_value,derive_env_var,group_cli_segment,flatten_sections, and theHelpMap/EnvMaptype aliases.Other changes
Parser._make_parserplumbs a one-line back-reference (parser.argclass_parser = self) soGenerateConfigActioncan recover the argclass parser from the argparse parser during invocation.INIConfigGeneratorskipsNonevalues (would otherwise reload as empty strings via configparser). Same behaviour now in TOML /.env.Tests
tests/test_generate_config.py— 75 tests covering:.env).list[T],bool,Optional,Secret(), explicitenv_var=,auto_env_var_prefix=, CLI args at every level).--generate-configcapturing CLI args + env vars.NonConfigActionopt-out (and inheritance from it).ConfigGeneratorsubclass (user-defined format).100% line/branch coverage on
argclass/emit.py. Existing tests untouched.Documentation
docs/config-generation.mdwith usage, source-precedence semantics,EnvConfigGeneratorwalkthrough,NonConfigActionrules, custom-format subclass example, securitynote, and a format-migration guide (one-shot script,
--generate-configmid-flight, bulk pipelines).README.mdwith quick examples.docs/llms.txtfor LLM consumers.docs/arguments.md"Argparse Passthrough Kwargs" section.docs/api.mdviaautoclass.python -m argclass genconfigwith four--generate-{ini,toml,json,env}flags so the feature is self-documenting.Limitations (intentional, documented)