root/trunk/examples/util/config/CustomSyntax.d

Revision 39, 2.6 kB (checked in by aarti_pl, 7 months ago)

- updated licences
- database package updates

Line 
1 /*******************************************************************************
2
3     License:    Boost Software License, v. 1.0
4                 Academic Free License, v. 3.0
5                 BSD License
6
7     Authors:    Marcin Kuszczak, www.zapytajmnie.com (author's christian site)
8                 This software is inspired and partially based on Boost C++
9                 library named 'program_options' and created by Vladimir Prus.
10
11     Version:    0.9.1
12     Date:       30-Apr-2008
13
14     History:    0.9.0 (08-Oct-2007) -   initial public version
15
16     Description:
17                 This example shows how to support custom options syntax.
18                 It's possible to install 'custom parser'. It will be invoked
19                 on all command line tokens and can return name/value pair, or
20                 nothing. If it returns nothing, usual processing will be done.
21
22  ******************************************************************************/
23
24 import std.stdio;
25 import std.string;
26
27 import doost.util.config.ProgramOptions;
28 import doost.util.config.CommandLineStorage;
29
30 //------------------------------------------------------------------------------
31
32 /*******************************************************************************
33     This custom option parse function recognize gcc-style
34     option "-ffoo" / "-fno-foo".
35  ******************************************************************************/
36 OptionPair reg_foo(char[] s) {
37     OptionPair result;
38     if (find(s, "-f") == 0) {
39         if (s[2..5] == "no-") {
40             result.name=s[5..$];
41             result.value="false";
42         }
43         else {
44             result.name=s[2..$];
45             result.value="true";
46         }
47     }
48     return result;
49 }
50
51 /*******************************************************************************
52     Program entry point
53  ******************************************************************************/
54 void main(char[][] args) {
55     try {
56         auto desc = new RegularOptions("Allowed options");
57         desc.options()
58             ("help", "produce a help message")
59             ("foo", define!(char[]), "just an option")
60         ;
61
62         auto po =   (new ProgramOptions).next(
63                         (new CommandLineStorage(args))
64                             .options(desc)
65                             .extraParser(delegate OptionPair (char[] s) { return reg_foo(s); })
66                     );
67
68         po.connect;
69
70         if ("help" in po) {
71             writefln(desc);
72             writefln("In addition -ffoo and -fno-foo syntax are recognized.");
73         }
74
75         if ("foo" in po) {
76             writefln("foo choosen with value: ", po["foo"]);
77         }
78
79         po.disconnect;
80     }
81     catch(ProgramOptionsException e) {
82         writefln(e);
83     }
84 }
Note: See TracBrowser for help on using the browser.