Skip to main content

This module adds a config file parsing capability to argparse.

Usage

Start by importing the module and initializing the parser:

import confargparse
parser = confargparse.ConfArgParser()

The usage is identical to the argparse module:

parser.add_argument("-n", type=int)
group = parser.add_argument_group("my group")
group.add_argument("-g")

Now, to use a configuration file (or list of sequentially read configuration files), just add the –conf-file option.

python prog.py –conf-file conf.ini

It is easy to write out a configuration file by applying all the options you want, and then adding the –export-conf-file option.

python prog.py -n –export-conf-file > conf.ini

API Changes

All argparse code should be compatible by just drapping in the new object. This package adds a few important options to the API to figure out how to map namespace dests to configuration sections/names.

The key concepts to note:

  1. Parameters in configuration files map to specific section/name pairs.

  2. Configuration file sections and names ignore case.

Specifiying the Name

By default all configuration names are the lowercase dest from argparse. Care must be taken to make sure that there are no name clashes from dests with different capitalizations.

The default name can be changed by using the “name” keyword to add_argument:

parser.add_argument("-n", type=int, name="my_n")

This targets the argument to “my_n” instead of “n” in the configuration file.

Specifying the Section

By default, all configurations go to the [defaults] section. Argument groups and subparsers inherit from the parser that initialized them.

The add_argument_group, add_argument, add_subparsers, and ConfArgParser initialization all include the “section” optional keyword argument. Specifying this section sets the section in the configuration the option will be targeted to. If the value is None, the object will inherit up as expected:

parser = ConfArgParser(section = "main")
parser.add_argument("-n", type=int)
group = parser.add_argument_group("my group", section="group")
group.add_argument("-g")
group.add_argument("-t", section="section2")

In this example, the first argument targets to “n” name in the [main] section. The second argument targets to the “g” name in the [group] section. The third argument targets to the “t” name in the [section2] section.

Excluding Arguments

Currently, positional arguments cannot be sent to the configuration file.

If you would like to exclude additional arguments, just use the exclude keyword argument to add_arguments:

parser.add_argument("-n", type=int, exclude=True)

Suggestions or BugFixes?

Feel free to contact me. I am findable online with a google search: S. Joshua Swamidass.

Please send bug fixes as pull requests to the bitbucket repository (https://bitbucket.org/swamidass/confargparse). Please keep pull requests clean, so I can easily figure out if it should be merged into the main line.

Metadata

Release files for ConfArgParse 1.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ConfArgParse 1.0.10
File Size Uploaded Provenance Actions
ConfArgParse-1.0.10.tar.gz 4.6 kB Without provenance Details for ConfArgParse-1.0.10.tar.gz

Release files / ConfArgParse-1.0.10.tar.gz

Download URL ConfArgParse-1.0.10.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
64cec4ceb3bf3d072e6e12c69e349a7a507036e9bf4720e49744b91726d763c2
BLAKE2b-256 checksum
How to use checksums
d0be4b233b18b0afd20a3d61c3bbecaeb8c029191a7e158f6d0b2e7c55d450a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for ConfArgParse 1.0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page