Package: cfgclasses
Config Classes (cfgclasses)CI/CDPackageMetaStrongly typed tool configuration classes for argument parsing.ConfigClasses are representations of a python tools CLI configuration options built ondataclasses. This allows individual tools to focus on specifying their configuration structure without the overhead of interacting with argparse and the typeless Namespace it returns.See thedocumnentationfor full user guides and API references.InstallationpipinstallcfgclassesFeaturesSimple class definitions using dataclassesNested config and tool submodes reduce code repetitionMutually exclusive groups supportValidation functions for reliable verification of configArgument transformations reducing repetition of common patterns (e.g. reading contents of a file)Example UsageThe following shows a simple script setup using a Config Class.importdataclassesimportsysfromcfgclassesimportConfigClass,arg,optionalfrompathlibimportPathfromtypingimportOptional@dataclasses.dataclassclassConfig(ConfigClass):# Simple options are required on the CLIintopt:int=arg("A simple integer field")inpath:Path=arg("A required Path field")# Optional fields default to None when not specifiedoutpath:Optional[Path]=optional("An optional Path field")# Can specify custom default or default_factory valuesstropt:str=arg("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.defrun(self)->None:"""Main entry point of this script."""...if__name__=='__main__':config=Config.parse_args(sys.argv[1:],prog="example")config.run()The-houtput 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 defaultThis same script implemented in argparse would look like this:importargparseimportsysfrompathlibimportPathdef_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 argparseparser.add_argument("--stropt",default=X", help="Astringfieldwithadefault")defmain(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 theexamplessubdirectory.LicenseCFG-Classes is distributed under the terms of theMITlicense.