ConfigArgParse is a drop-in replacement for Python's standard `argparse` module, enhancing it with the ability to load configuration options from command-line arguments, environment variables, and configuration files (INI, YAML, TOML formats). It offers a unified API to define, document, and parse settings from multiple sources with a clear precedence order (command line > environment variables > config file values > defaults). The library is actively maintained, with its current version being 1.7.5.
pip install configargparseVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define arguments and have them automatically loaded from a default config file and environment variables, with command-line arguments taking the highest precedence. It sets up a basic `ArgumentParser`, defines a config file and an environment variable, then parses the arguments to show the effective values based on precedence. It also shows how to add an argument to specify an alternative config file.
Upgrade to v1.7.4 or later. For older versions, explicitly pass environment variable values as command-line arguments to the subparser, or ensure critical arguments are defined directly on subparsers with `env_var` if not relying on `auto_env_var_prefix`.
Upgrade to v1.7.3 or later. These versions contain fixes for proper handling of `nargs=REMAINDER` and the `--` separator, ensuring config file arguments are inserted correctly into the argument list.
Ensure that arguments passed to the `ArgumentParser` constructor adhere to the expected types (e.g., `config_file_parser_class` must be a subclass of `ConfigFileParser`, lists/tuples for file path arguments). Refer to the documentation for correct parameter types.
Understand the 'Special Values' handling. For boolean flags (`action='store_true'`), `key=true` is interpreted as `"--key"` (setting it to True). `key=false` or omitting the key will result in `False`. For lists, `key = [item1, item2]` is generally supported by modern parsers (e.g., `TomlConfigParser`), or ensure your parser handles multiple lines if you intend to append.
Always define a long argument (`--my-option`) if you intend for an option to be configurable via a config file. The corresponding config file key can then be `my-option` or `--my-option`.
Upgrade to v1.7.3 or later to benefit from fixes addressing these parsing inconsistencies and `SyntaxError` issues for TOML and INI formats.
Install the library using pip: `pip install configargparse`
When defining the argument for the config file, set `is_config_file=True`. Example: `parser.add_argument('-c', '--config-file', is_config_file=True, help='Path to configuration file')`Review the specified YAML (or INI/TOML) configuration file for syntax errors, incorrect indentation, unquoted strings, or invalid character sequences. Ensure the file adheres to the YAML specification (or INI/TOML). For YAML, using a YAML linter can help identify issues.
Ensure that the values for the `action='append'` argument with `nargs > 1` are specified as a list of lists or an equivalent structure in the config file, providing the correct number of items for each append. For example, in YAML: `indexes: [[2, 4], [1, 9]]`.
Either provide the required argument directly on the command line, or, if you intend for the config file value to satisfy the requirement, you might need to adjust the argument definition or logic to explicitly handle required values from config files, perhaps by making them not `required=True` but checking their presence after parsing.