Configuration
Some scripts might rely on options that can be configured by the user. Instead of manually editing constants in the script, one can use the configuartion file.
Using command-line options
Most programming languages a capable of reading arguments provided in the command-line. Rust uses these method to allow users to provide options specific to a script in the configuration file.
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("name")
parser.add_argument("age", type=int)
args = parser.parse_args()
print(f"You are {args.name} and you are {args.age} years old.")
One can then provide arguments in their configuration file like this:
[scripts]
base-path = "scripts"
pre = [
{ run = "helloworld.py", with = "python", args = ["John Doe", "42"] }
]
Remember to document the arguments somewhere.
Parsing Libraries
Using [metadata]
Another less recommended approach is the usage of the
[metadata]
section in the configuration
file. It is designed to be used by external programs but parsing TOML is less
supported than parsing command-line arguments in many programming languages.
import os
from pathlib import Path
import tomllib
config_file = Path(os.environ["ALLAY_CONFIG"])
with config_file.open("rb") as f:
data = tomllib.load(f)["metadata"]["helloworld"]
print(f"You are {data['name']} and you are {data['age']} years old.")
One can then provide arguments in their configuration file like this:
[metadata.helloworld]
name = "John Doe"
age = 42