Add config_argument: user-supplied config file as argument defaults#48
Merged
Conversation
config_files= lets the developer preset defaults from files chosen at
construction time. The new Parser(config_argument="--config") adds a
CLI flag so the END USER can point at a config file whose values
become argument defaults, resolved at invocation time.
Implementation is two-pass parsing: a throwaway pre-scan parser (that
neither prints nor exits) extracts only the config path from argv;
the file is loaded through the shared config_parser_class machinery
and layered over the constructor config defaults; then the real
parser is built — so required-relaxation, type-aware conversion,
group sections, and --help defaults all come from the existing
defaults pipeline.
Priority chain extends naturally:
declared defaults < config_files < config_argument file < env < CLI
Properties of the feature:
- accepts one flag or several aliases: config_argument=("-c", "--config")
- an explicitly passed path that is missing or malformed raises
ConfigurationError (config_files stays a lenient search list)
- no cascade into subparsers (consistent with config_files)
- runtime defaults live only for the duration of one parse_args call
- new Parser.loaded_config_files property reports applied files in
priority order
Also fixes a related pre-existing bug in ConfigAction: an explicit
--config file was merged BEFORE search_paths, so preset files
silently overrode the user's explicit choice; the explicit file now
wins.
Python 3.14 argparse colorizes usage/help output and CI sets FORCE_COLOR=1, so ANSI escapes broke substring assertions on help text (the new test_flag_appears_in_help was the first such assert). PYTHON_COLORS takes precedence over FORCE_COLOR, making captured output deterministic everywhere.
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
config_files=lets the developer preset defaults from files chosen atconstruction time. The new
Parser(config_argument="--config")adds a CLIflag so the end user can point at a config file whose values become
argument defaults, resolved at invocation time:
Priority chain extends naturally:
Implementation
Two-pass parsing (the classic argparse chicken-and-egg: defaults must be known
before parsing, but the config path arrives as an argument):
the config flag value from argv (syntax errors are left for the real parser
to report with proper usage text).
config_parser_classmachinery(INI/JSON/TOML or a custom
AbstractDefaultsParser) and layered over theconstructor config defaults.
conversion, group/nested-group sections, and
--helpdefaults all comefrom the existing defaults pipeline.
Details:
ConfigurationError— unlikeconfig_files, which stays a lenient searchlist.
parse_args()call (no staleness between parses).Parser.loaded_config_filesproperty reports applied files in priorityorder.
config_files).Also fixes a related pre-existing bug in
ConfigAction(argclass.Config()):an explicit
--configfile was merged beforesearch_paths, so presetfiles silently overrode the user's explicit choice. The explicit file now
wins.
Tests & docs
tests/test_config_argument.py): the priority chainpair-by-pair, required-satisfied-by-config, nested group sections, JSON
parser class, aliases/
=-form, help output, error cases, re-parseisolation, and the
ConfigActionordering fix.config-files.md(executable example), README example,api.mdcross-references,Parser.__init__docstring (flows into the APIreference via autodoc), feature bullet in
index.md, pitfalls table row.Verification
tomllibbaseline only);coverage at the 100% gate; Sphinx builds without warnings.