Dataclass fields as cmd args and env vars
Project description
dcflags
Python package for initializing a dataclass with values from the command line and from environmental variables.
Install
dcflags is available through pip
pip install dcflags
Usage
# main.py
import dataclasses
import dcflags
@dataclasses.dataclass
class Config:
output: str
workers: int = 1
verbose: bool = False
if __name__ == "__main__":
cfg = dcflags.parse(Config)
print(cfg)
# help message
$ python main.py --help
usage: main.py [-h] [--output OUTPUT] [--workers WORKERS] [--verbose [VERBOSE]]
options:
-h, --help show this help message and exit
--output OUTPUT type: str, env: $OUTPUT
--workers WORKERS type: int, env: $WORKERS, default: 1
--verbose [VERBOSE] type: bool, env: $VERBOSE, default: False
# missing required argument
$ python main.py
usage: main.py [-h] [--output OUTPUT] [--workers WORKERS] [--verbose [VERBOSE]]
main.py: error: the following arguments are required: --output/$OUTPUT
# command line arguments
$ python main.py --output=file.txt
Config(output='file.txt', workers=1, verbose=False)
# env vars
$ OUTPUT=test.txt python main.py
Config(output='test.txt', workers=1, verbose=False)
# a bit of everything
$ OUTPUT=new.txt python main.py --verbose --workers=3
Config(output='new.txt', workers=3, verbose=True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dcflags-0.1.0.tar.gz
(4.9 kB
view details)
Built Distribution
File details
Details for the file dcflags-0.1.0.tar.gz
.
File metadata
- Download URL: dcflags-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56216612e9557aa2ca1cc486c4f119b3b10a593e65866a37571ba182af419570 |
|
MD5 | 81c2c99998cf2b1b77604ec386069cd4 |
|
BLAKE2b-256 | 026b767e2fc388d183b37b38952989ebb2c4c78d7a02bf506f61680fcc80af8a |
File details
Details for the file dcflags-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: dcflags-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd7833da4106e96490b309c9419b7a62e5158050640b207a497c516d1c1a4247 |
|
MD5 | 6cbaf60aee097ffeda229902fe1483a2 |
|
BLAKE2b-256 | 8ff5de7c1fed78aabf9d67c6ccdac1b01502cdf5a347c9e78802dcb5d0758ecf |