Skip to main content

configparse

Build Status Code style: black

Summary

configparse is a drop-in-place replacement for python's argparse module which reads configuration files in addition to command line arguments.

Features

If the configuration directory exists, all of files in <config_directory>/<prog>/ are read. All files matching ~/.<prog>* are read.

Files are treated according to their extension: config.json will be parsed as JSON, myconf.yml will be parsed as YAML.

If a file does not have an extension, it will be parsed according to the default file format. This is preset to JSON, but can be changed by applications.

Configuration Directory

On Windows, the configuration directory looked up in %AppData%. On any other OS, it is looked up from $XDG_CONFIG_HOME. If the variable is not present (on any OS), files are read from ~/.config.

Supported Formats

The following file formats are currently supported:

  • JSON (.json)
  • YAML (.yml)
  • INI (.ini)
  • TOML (.toml)

Feel free to submit a pull request adding more formats. See CONTRIBUTING.md for an overview, if you already have a parser it should be very simple.

Using configparse

As a library

If you are an application developer who wants to use the library, there is one entry point: the ConfigParser class, which is also aliased to Parser for convenience. You can treat a ConfigParser instance exactly as you would an ArgumentParser instance (as long as you pass prog to the constructor).

Note that this means that parse_args will parse sys.argv in addition to parsing configuration files. If you don't want this behavior, pass an empty list to parse_args like this: args = parser.parse_args([]).

If you want to change the default file format (e.g. for files named ~/.myprog without an extension), use parser.set_default_ext(ext), where 'ext' is the file extension for your format.

Differences from argparse

The only API differences are as follows:

  • the name of the parser is ConfigParser, not ArgumentParser
  • the prog keyword is required for initializing ConfigParser (so that it knows where to look for configuration files)

That's it. Everything else is done automatically.

Example

from configparse import Parser

parser = Parser(prog='myprogram')
parser.add_argument('--short', '-s', help='use short format')
args = parser.parse_args()

As an end user

You can put a configuration file in any of the following locations:

  • ~/.config/<prog>/
  • ~/.<prog>*

See Features for a full overview.

Example

Assume that the program basic takes the arguments '--short' and '--long'.

$ cat ~/.config/basic/config.yml
short: true
$ basic
You chose the 'short' option.
$ rm ~/.config/basic/config.yml
$ echo 'long: true' > ~/.basic.yml
$ basic
You chose the 'long' option.

Limitations

  • Only long options will be applied. If a short option is present in a config file, it will be treated as if it were a long option, i.e. not looked up. For example, assuming some backend returns the dictionary {'s': 'some value'}:
from configparse import Parser

p = Parser(prog='myprog')
p.add_argument('-s', '--short')
args = p.parse_args()
assert args.short is None
assert args.s == 'some value'

Metadata

Release files for configparse 0.1.5

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

Source distribution (sdist)

Source distribution for configparse 0.1.5
File Size Uploaded
configparse-0.1.5.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for configparse 0.1.5
File Interpreter ABI Platform
configparse-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / configparse-0.1.5.tar.gz

Download URL configparse-0.1.5.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
60c13a99aaa76207425f00ad997d7849894596ab8c21c6b7e7c3cbf75135932c
BLAKE2b-256 checksum
How to use checksums
ec1b78550818f8cae3a9bae3e1e4632e849c6e1edb9917746b05dcc2827c0baa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release files / configparse-0.1.5-py3-none-any.whl

Download URL configparse-0.1.5-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ddeb41045dc2fa1fe34f4c52ade06dbb69e0c8822256f43522b6fe2e29bec57a
BLAKE2b-256 checksum
How to use checksums
5bee0bf6d28248c0cf5b5820405693952f611ce6696b65d289cd1bc1781969f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

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