Skip to main content

Strongly typed tool configuration classes for argument parsing.

Project description

Config Classes (cfgclasses)

CI/CD CI - Test
Package PyPI - Version PyPI - Downloads PyPI - Python Version
Meta code style - Black types - Mypy linting - pylint License - MIT

Strongly typed tool configuration classes for argument parsing.

ConfigClasses are representations of a python tools CLI configuration options built on dataclasses. This allows individual tools to focus on specifying their configuration structure without the overhead of interacting with argparse and the typeless Namespace it returns.

Installation

pip install cfgclasses

Features

  • Simple class definitions using dataclasses
  • Nested config and tool submodes reduce code repetition
  • Mutually exclusive groups support
  • Validation functions for reliable verification of config

Example Usage

The following shows a simple script setup using a Config Class.

import cfgclasses as cfg
import dataclasses
import sys
from pathlib import Path
from typing import Optional

@dataclasses.dataclass
class Config(cfg.ConfigClass):
    # Simple options are required on the CLI
    intopt: int = cfg.simple("A simple integer field")
    inpath: Path = cfg.simple("A required Path field")

    # Optional fields default to None when not specified
    outpath: Optional[Path] = cfg.optional("An optional Path field")

    # Can specify custom default or default_factory values
    stropt: str = cfg.simple("A string field with a default", default="X")

    # The authors preference is to put run modes on the config classes.
    # This is entirely optional, and main() methods that take in the Config
    # object as their only arg is a perfectly sensible alternative.
    def run(self) -> None:
        """Main entry point of this script."""
        ...

if __name__ == '__main__':
    config = Config.parse_args(sys.argv[1:], prog="example")
    config.run()

The -h output from this script is:

usage: example [-h] --intopt INTOPT --inpath INPATH [--outpath OUTPATH] [--stropt STROPT]

options:
  -h, --help         show this help message and exit

  --intopt INTOPT    A simple integer field
  --inpath INPATH    A required Path field
  --outpath OUTPATH  An optional Path field
  --stropt STROPT    A string field with a default

This same script implemented in argparse would look like this:

import argparse
import sys
from pathlib import Path

def _parse_args(argv: list[str]):
    parser = argparse.ArgumentParser(prog="example")
    # In argparse options default to None unless you specified they're required.
    # Help messages are optional in argparse, but required in cfgclasses.
    parser.add_argument("--intopt", type=int, help="A simple integer field", required=True)
    parser.add_arugment("--inpath", type=Path, help="A required Path field", required=True)

    # Optional fields are the default in argparse, so type is actually Optional[Path]
    parser.add_argument("--outpath", type=Path, help="An optional Path field")

    # Defaults work the same, but there's no default_factory in argparse
    parser.add_argument("--stropt", default=X", help="A string field with a default")

def main(args: argparse.Namespace) -> None:
    """Main entry point of this script."""
    # Note: args here is relatively typeless - if one of the argument types is
    # changed (e.g. from str to Path) mypy will not pick up on this error.
    ...

if __name__ == '__main__':
    args = _parse_args(sys.argv[1:])
    main(args)

For further examples see the examples subdirectory.

License

CFG-Classes is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cfgclasses-1.1.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

cfgclasses-1.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file cfgclasses-1.1.0.tar.gz.

File metadata

  • Download URL: cfgclasses-1.1.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for cfgclasses-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3be9c4decdfa650a11a0fa91229f4007ee445b4b8fa5d8264e90793aab07901d
MD5 ad1fa6b8d3ee9b5fdfad5c7810e784f5
BLAKE2b-256 27a1970f42bb3bc3aff8e80b8d88cb5a97f1465728bc0372346d7cea8e8a294c

See more details on using hashes here.

File details

Details for the file cfgclasses-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: cfgclasses-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for cfgclasses-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5868384b4a8b16130b03bfd6a5573c18df767cd0133fd4e616105daaf38eadaa
MD5 97493644875568e5d8bff3b9a7b0cc6f
BLAKE2b-256 1a423e3ca73769e1acdd2380f4a31778ca88acc1406d352136a47a984dae3369

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page