Configpile: get clean configuration out of a pile of arguments
Contents
Configpile: get clean configuration out of a pile of arguments¶
Configpile is a replacement for the argparse module of the Python
standard library.
It is written in clean, modern Python with extensive support of
PEP 484 type hints and dataclasses.
It processes configuration settings coming from a variety of sources:
Environment variables
Command-line parameters
Configuration files (INI)
Its best selling points are:
Code compactness; from the same dataclass declaration, we derive a configuration parser, a command-line usage description and a Sphinx documentation block.
Compatibility with IDEs (due to typed dataclasses as configuration results)
Error reporting: Instead of crashing at the first parse error, configpile collects all the errors encountered and reports all of them.
How to use¶
configpile is easily installed through pip:
pip install configpile
It supports Python >= 3.8, depends only on
class-doc and typing_extensions.
Optionally, it supports parsy (to define more complicated parsers) and rich (for pretty-printing error information).
You can do:
pip install configpile[parsy,rich]
Code example¶
(taken from Step 2: Reuse defaults, short flags)
"""
Calculator tutorial, step 2
"""
from dataclasses import dataclass
from typing_extensions import Annotated
from configpile import Config, Param, parsers
@dataclass(frozen=True)
class Calc(Config):
"""
Command-line tool that sums two floating point numbers
"""
#: First argument
x: Annotated[float, Param.store(parsers.float_parser, short_flag_name="-x")]
#: Second argument
y: Annotated[float, Param.store(parsers.float_parser, short_flag_name="-y")]
c = Calc.from_command_line_()
print(f"{c.x} + {c.y} = {c.x+c.y}")
Execution result¶
$ python ../../examples/calculator2.py --help
usage: ../../examples/calculator2.py [-x X] [-y Y]
Command-line tool that sums two floating point numbers
required arguments:
-x X, --x X First argument
-y Y, --y Y Second argument
$ python ../../examples/calculator2.py -x 1 -y 2
1.0 + 2.0 = 3.0