App/lib config system

Jacob Carlborg doob at me.com
Wed Jul 8 08:17:35 UTC 2020


I'm looking for a way to configure applications and libraries. 
I'm imagining a way for libraries to hook into a single point to 
provide the configuration parameters the library provides. These 
parameters can then be changed in one location from the user of 
the libraries (usually an application). My main requirement is 
that it should be possible to configure compile time values. I 
think the best way to explain what I'm looking for is with an 
example:

module log;

import config;

void log(Level level, string message)
{
     static if (config.log.isEnabled)
     {
         if (config.log.level >= level)
             writeln(message);
     }
}

In the above example, the code would be part of the "log" 
library. The configuration parameters `config.log.isEnabled` and 
`config.log.level` can be changed by the application that is 
using the "log" library. `config.log.isEnabled` is a compile time 
value, that can only be changed at compile time. 
`config.log.level` is a runtime value that can be changed at 
runtime as well.

Here's a list of my ideal requirements:

* Should support both compile time and runtime config parameters
* Arbitrary libraries should be able to hook into config system 
to provide it with its config parameters
* There should be one place to change the config
* The "config file" should be a regular D module
* It should work with Dub. In the above example the "log" library 
and the application would be in separate Dub packages.

I've done some experiments and I have some pieces working, but 
have not been able to glue everything together. I don't mind if 
there are any dirty tricks behind the scenes but I would like it 
to be as easy as the above example to use it, if possible.

Does anyone have a system like this that is already available?

--
/Jacob Carlborg



More information about the Digitalmars-d-learn mailing list