Dataclass fields as cmd args and env vars
Project description
dcflags
Python package for initializing a dataclass with arguments from the command line or values from environmental variables. Meant to work as an entrypoint for applications, nothing else.
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.2.1.tar.gz
(5.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dcflags-0.2.1.tar.gz.
File metadata
- Download URL: dcflags-0.2.1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12f41827ba49c395c76eba0ff55d35850c6e0d1eaab5b70200a8520e6175e6ff
|
|
| MD5 |
7248782f2d76b5951bbae2fdc432623b
|
|
| BLAKE2b-256 |
2e6a65ce00234329ac22623f89eaba12bf0a6d198b87fa4291ca3623172dba2c
|
File details
Details for the file dcflags-0.2.1-py3-none-any.whl.
File metadata
- Download URL: dcflags-0.2.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc7bf6c7409b08381fae28481d5bab5b51451f38c67d3cc544f637b674803d3c
|
|
| MD5 |
f181e1b09a1b832df85940db0832091c
|
|
| BLAKE2b-256 |
eea3b3981a1d407fd63ab7a1e1a8c4d072b73bd747218c7c6170f91b342b78e7
|