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     Returns:
26         CLI argument array with processed flags removed
27 */
28 string[] readConfiguration (S) (ref S dst)
29 {
30     static assert (is(S == struct), "Only structs are supported as configuration target");
31 
32     static import simpleconfig.file;
33     simpleconfig.file.readConfiguration(dst);
34 
35     static import simpleconfig.args;
36     auto args = simpleconfig.args.readConfiguration(dst);
37 
38     static if (is(typeof(S.finalizeConfig())))
39         dst.finalizeConfig();
40 
41     return args;
42 }
43 
44 ///
45 unittest
46 {
47     void example ()
48     {
49         struct S
50         {
51             @cli
52             int value;
53             @cli("flag|f") @cfg("flag")
54             string anothervalue;
55 
56             void finalizeConfig () { }
57         }
58 
59         S s;
60         readConfiguration(s);
61     }
62 }