I am using OmegaConf
's structured config system in order to get a convenient, strongly typed way of configuring my Python application, since it takes very few lines of code and allows merging confs from source, filesystem and CLI. This is defined through a ConfDef
class like the one below.
Now, I want to provide a parameter as a collection of, say, strings. While the OmegaConf
documentation addresses how should the ConfDef
look like, no details about the CLI interaction are provided. Hence the question:
I am using Ubuntu20.04, but generic approaches if existing would be preferred.
from dataclasses import dataclass
from typing import List
from omegaconf import OmegaConf, MISSING
@dataclass
class ConfDef:
"""
:cvar MY_PATHS: Collection of paths to be processed
"""
MY_PATHS: List[str] = MISSING
python my_scripy.py MY_PATHS="[a,b,c]"
Then, inside Python, we can instantiate and access the conf e.g. as follows:
conf = OmegaConf.structured(ConfDef())
cli_conf = OmegaConf.from_cli()
conf = OmegaConf.merge(conf, cli_conf)
print("\n\nCONFIGURATION:")
print(OmegaConf.to_yaml(conf), end="\n\n\n")
print(conf.MY_PATHS) # This prints the list of strings ["a", "b", "c"]
Furthermore, if you want to expand a glob
pattern with multiple paths, e.g. ./data/files/*
, this can be achieved as follows:
# just basenames
python my_script.py MY_PATHS="[`ls -p data/files/ | grep -v / | tr '\n' ','`]"
# absolute paths (nonrecursive)
python my_script.py MY_PATHS="[`EXCEL_PATHS="[`ls -d $PWD/data/files/* | tr '\n' ','`]"`]"
Explanation (from here):
ls
lists the files in the pathgrep
flattens the ls
into a newline-separated listtr
: replaces the newlines with commasYielding the collection in the desired form above.