Library primarily intended to help quickly write small command-line tools. Parses command-line arguments and configuration file and stores result into provided D struct instance.
void main ()
{
import simpleconfig;
struct Config
{
@cli
int value;
@cli("flag|f") @cfg("flag")
string anothervalue;
@cfg("some ratio")
double ratio;
void finalizeConfig ()
{
}
}
Config config;
readConfiguration(config);
}
This snippet will do the following:
- Check standard locations for the current platform for the config file named
appname.cfg
. If found, parse it and initializeconfig.anothervalue
fromflag
entry andconfig.ration
fromsome ratio
entry. - Iterate through command-line arguments. If
--value
argument is found, it will be written toconfig.value
(usingstd.conv.to
for conversion). If either--flag
or-f
arguments are found, it will be written toconfig.anothervalue
. - Call
config.finalizeConfig
if defined for a given struct.
If value is set both by config file and command-line argument, the latter takes priority.
Currently config file is named appname.cfg
and uses simple key-value format:
key = value
another key = " value that starts with a whitespace"
The following locations are checked for config file presence:
- Current working directory
- Same folder as the executable
$XDG_CONFIG_HOME
(Posix) or%LOCALAPPDATA%
(Windows)