| 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 handle options groups. |
|---|
| 18 |
|
|---|
| 19 |
For a test, run: |
|---|
| 20 |
option_groups --help |
|---|
| 21 |
option_groups --num-threads 10 |
|---|
| 22 |
option_groups --help-module backend |
|---|
| 23 |
|
|---|
| 24 |
The first invocation would show to option groups, and will not |
|---|
| 25 |
show the '--num-threads' options. The second invocation will |
|---|
| 26 |
still get the value of the hidden '--num-threads' option. |
|---|
| 27 |
Finally, the third invocation will show the options for the |
|---|
| 28 |
'backend' module, including the '--num-threads' option. |
|---|
| 29 |
|
|---|
| 30 |
******************************************************************************/ |
|---|
| 31 |
|
|---|
| 32 |
import std.stdio; |
|---|
| 33 |
|
|---|
| 34 |
import doost.util.config.ProgramOptions; |
|---|
| 35 |
import doost.util.config.CommandLineStorage; |
|---|
| 36 |
|
|---|
| 37 |
//------------------------------------------------------------------------------ |
|---|
| 38 |
|
|---|
| 39 |
/******************************************************************************* |
|---|
| 40 |
Program entry point |
|---|
| 41 |
******************************************************************************/ |
|---|
| 42 |
void main(char[][] args) { |
|---|
| 43 |
try { |
|---|
| 44 |
// Declare three groups of options. |
|---|
| 45 |
auto general = new RegularOptions("General options"); |
|---|
| 46 |
general.options() |
|---|
| 47 |
("help", "produce a help message") |
|---|
| 48 |
("help-module", define!(char[]), "produce a help for a given module") |
|---|
| 49 |
("version", "output the version number") |
|---|
| 50 |
; |
|---|
| 51 |
|
|---|
| 52 |
auto gui = new RegularOptions("GUI options"); |
|---|
| 53 |
gui.options() |
|---|
| 54 |
("display", define!(char[]), "display to use") |
|---|
| 55 |
; |
|---|
| 56 |
|
|---|
| 57 |
auto backend = new RegularOptions("Backend options"); |
|---|
| 58 |
backend.options() |
|---|
| 59 |
("num-threads", define!(int), "the initial number of threads") |
|---|
| 60 |
; |
|---|
| 61 |
|
|---|
| 62 |
// Declare an options description instance which will include |
|---|
| 63 |
// all the options |
|---|
| 64 |
auto all = new RegularOptions("Allowed options"); |
|---|
| 65 |
all.add(general, gui, backend); |
|---|
| 66 |
|
|---|
| 67 |
// Declare an options description instance which will be shown |
|---|
| 68 |
// to the user |
|---|
| 69 |
auto visible = new RegularOptions("Allowed options"); |
|---|
| 70 |
visible.add(general).add(gui); |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
auto po = (new ProgramOptions) |
|---|
| 74 |
.next( |
|---|
| 75 |
new CommandLineStorage(args, all) |
|---|
| 76 |
); |
|---|
| 77 |
|
|---|
| 78 |
po.connect; |
|---|
| 79 |
|
|---|
| 80 |
auto formatter = new CommandLineFormatter; |
|---|
| 81 |
|
|---|
| 82 |
if ("help" in po) { |
|---|
| 83 |
writefln(visible.describe(formatter)); |
|---|
| 84 |
return; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
if ("help-module" in po) { |
|---|
| 88 |
char[] s = po["help-module"].as!(char[])(); |
|---|
| 89 |
switch(s) { |
|---|
| 90 |
case "gui": writefln(gui.describe(formatter)); break; |
|---|
| 91 |
case "backend": writefln(backend.describe(formatter)); break; |
|---|
| 92 |
default: writefln("Unknown module '", s, "' in the --help-module option"); |
|---|
| 93 |
return 1; |
|---|
| 94 |
|
|---|
| 95 |
} |
|---|
| 96 |
return; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
if ("num-threads" in po) { |
|---|
| 100 |
writefln("The 'num-threads' options was set to ", po["num-threads"].as!(int)()); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
po.disconnect; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
catch(ProgramOptionsException e) { |
|---|
| 107 |
writefln(e); |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|