1 /** 2 Check README.md for a usage example and abstract description. 3 */ 4 module simpleconfig; 5 6 public import simpleconfig.attributes; 7 8 /** 9 Reads a configuration data from a config file and 10 command-line arguments. 11 12 Command-line arguments will override config file entries. 13 Config file will override defaults. Only fields marked with 14 @cli and/or @cfg UDA will be updated, any other symbols will be ignored. 15 16 Configuration file location will be checked in this order: 17 - current working directory 18 - same folder as the binary 19 - $XDG_CONFIG_HOME/appname.cfg (Posix) or %LOCALAPPDATA%/appname.cfg (Windows) 20 21 Params: 22 dst = struct instance which will store configuration data 23 and defines how it should be read 24 */ 25 void readConfiguration (S) (ref S dst) 26 { 27 static assert (is(S == struct), "Only structs are supported as configuration target"); 28 29 static import simpleconfig.file; 30 simpleconfig.file.readConfiguration(dst); 31 32 static import simpleconfig.args; 33 simpleconfig.args.readConfiguration(dst); 34 35 static if (is(typeof(S.finalizeConfig()))) 36 dst.finalizeConfig(); 37 } 38 39 /// 40 unittest 41 { 42 void example () 43 { 44 struct S 45 { 46 @cli 47 int value; 48 @cli("flag|f") @cfg("flag") 49 string anothervalue; 50 51 void finalizeConfig () { } 52 } 53 54 S s; 55 readConfiguration(s); 56 } 57 }