Config
User guide: Config.
Usage
from clak import Parser, XDGConfigMixin
class App(XDGConfigMixin, Parser):
class Meta:
app_name = "cool-cli"
# config_required = True # fail if file is missing
def cli_run(self, ctx, **_):
# dict from file (or {} if missing)
print(ctx.config)
# attribute access on the root parser
print(self.config.get("debug"))
if __name__ == "__main__":
App()
- JSON (
.json): always available (stdlib). - YAML (
.yaml/.yml): requires optional extra:pip install 'mrjk.clak[config]'. - Missing file → empty config unless
Meta.config_required = True. - Config is not merged into CLI args; read
ctx.config/self.configexplicitly.
clak.comp.config
XDG Base Directory path helpers and config-file loading.
Provides XDGConfigMixin so apps can expose standard config/data/cache/log
path flags with defaults from Meta.app_name / $XDG_*, and load
--conf-file once via cli_hook__config.
XDGConfigMixin
XDG path flags and config-file loading.
Adds:
- --conf-file: $XDG_CONFIG_HOME/<app>/config.yaml
- --data-dir: $XDG_DATA_HOME/<app> (hidden)
- --cache-dir: $XDG_CACHE_HOME/<app> (hidden)
- --log-dir: $XDG_CACHE_HOME/<app>/logs (hidden)
<app> comes from Meta.app_name, else the parser name / class name.
Defaults respect $XDG_CONFIG_HOME, $XDG_DATA_HOME, and
$XDG_CACHE_HOME when set.
On dispatch, cli_hook__config loads --conf-file (JSON always;
YAML with the config extra). Missing file yields {} unless
Meta.config_required is true. Loaded data is available as
ctx.config (dict) and cli_root.config (attribute namespace).
Source code in clak/comp/config.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
add_arguments(arguments=None)
Apply XDG defaults from app name / env, then register arguments.
Source code in clak/comp/config.py
cli_hook__config(instance, ctx, **_)
Load --conf-file once and expose it on ctx / root.
Source code in clak/comp/config.py
load_config_file(path)
Load a mapping from a JSON or YAML config file.
Format is detected from the file suffix (.json, .yaml, .yml).
YAML requires the optional config extra (PyYAML).
Raises:
| Type | Description |
|---|---|
ClakUserError
|
Unknown suffix, missing PyYAML, I/O/parse error, or non-mapping root document. |
Source code in clak/comp/config.py
resolve_xdg_paths(app_name)
Build conf/data/cache/log paths for app_name under XDG bases.
Source code in clak/comp/config.py
sanitize_xdg_app_name(name)
Turn an app name into a safe path segment under XDG directories.
xdg_dir(env_var, default=None)
Resolve an XDG base directory from the environment.
Uses $env_var when set and non-empty; otherwise expands default
(or the XDG Base Directory default for that variable).