| 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 |
Shows how to use both command line and config file. |
|---|
| 18 |
|
|---|
| 19 |
******************************************************************************/ |
|---|
| 20 |
|
|---|
| 21 |
import std.stdio; |
|---|
| 22 |
import std.file; |
|---|
| 23 |
|
|---|
| 24 |
import doost.util.config.ProgramOptions; |
|---|
| 25 |
import doost.util.config.CommandLineStorage; |
|---|
| 26 |
import doost.util.config.ConfigFileStorage; |
|---|
| 27 |
|
|---|
| 28 |
//------------------------------------------------------------------------------ |
|---|
| 29 |
|
|---|
| 30 |
/******************************************************************************* |
|---|
| 31 |
Program entry point |
|---|
| 32 |
******************************************************************************/ |
|---|
| 33 |
void main(char[][] args) { |
|---|
| 34 |
try { |
|---|
| 35 |
// Declare a group of options that will be |
|---|
| 36 |
// allowed only on command line |
|---|
| 37 |
auto generic = new RegularOptions("Generic options"); |
|---|
| 38 |
generic.options() |
|---|
| 39 |
("version,v", "print version string") |
|---|
| 40 |
("help,h", "produce help message") |
|---|
| 41 |
; |
|---|
| 42 |
|
|---|
| 43 |
auto cmdline = new CommandLineOptions(); |
|---|
| 44 |
cmdline.options() |
|---|
| 45 |
("selfdir", new SelfDir) |
|---|
| 46 |
("selfname", new SelfName) |
|---|
| 47 |
; |
|---|
| 48 |
|
|---|
| 49 |
// Declare a group of options that will be |
|---|
| 50 |
// allowed both on command line and in |
|---|
| 51 |
// config file |
|---|
| 52 |
auto config = new RegularOptions("Configuration"); |
|---|
| 53 |
config.options() |
|---|
| 54 |
("optimization", define!(int)().defaultValue(10), "optimization level") |
|---|
| 55 |
("include-path,I", define!(char[][])().composing(), "include path") |
|---|
| 56 |
; |
|---|
| 57 |
|
|---|
| 58 |
// Hidden options, will be allowed both on command line and |
|---|
| 59 |
// in config file, but will not be shown to the user. |
|---|
| 60 |
auto hidden=new RegularOptions("Hidden options"); |
|---|
| 61 |
hidden.options() |
|---|
| 62 |
("input-file", define!(char[][]), "input file") |
|---|
| 63 |
; |
|---|
| 64 |
|
|---|
| 65 |
auto cmdline_options = new RegularOptions; |
|---|
| 66 |
cmdline_options.options() |
|---|
| 67 |
(generic) |
|---|
| 68 |
(config) |
|---|
| 69 |
(hidden) |
|---|
| 70 |
; |
|---|
| 71 |
|
|---|
| 72 |
auto config_file_options = new RegularOptions; |
|---|
| 73 |
config_file_options.add(config).add(hidden); |
|---|
| 74 |
|
|---|
| 75 |
auto visible = new RegularOptions("Allowed options"); |
|---|
| 76 |
visible.add(generic).add(config); |
|---|
| 77 |
|
|---|
| 78 |
auto p = new CommandLineOptions; |
|---|
| 79 |
p.options() |
|---|
| 80 |
("input-file=*", new PositionalOption) |
|---|
| 81 |
; |
|---|
| 82 |
|
|---|
| 83 |
auto po = (new ProgramOptions) |
|---|
| 84 |
.next( |
|---|
| 85 |
(new CommandLineStorage(args)) |
|---|
| 86 |
.options(cmdline_options, cmdline) |
|---|
| 87 |
.next( |
|---|
| 88 |
(new ConfigFileStorage("MultipleSources.cfg")) |
|---|
| 89 |
.options(config_file_options) |
|---|
| 90 |
)); |
|---|
| 91 |
|
|---|
| 92 |
po.connect; |
|---|
| 93 |
|
|---|
| 94 |
if ("help" in po) { |
|---|
| 95 |
writefln(visible); |
|---|
| 96 |
return 0; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
if ("version" in po) { |
|---|
| 100 |
writefln("Multiple sources example, version 1.0"); |
|---|
| 101 |
return 0; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
if ("include-path" in po) { |
|---|
| 105 |
writefln("Include paths are: ", po["include-path"]); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
if ("input-file" in po) { |
|---|
| 109 |
writefln("Input files are: ", po["input-file"]); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
writefln("Optimization level is ", po["optimization"]); |
|---|
| 113 |
writefln("Selfdir is ", po["selfdir"]); |
|---|
| 114 |
writefln("Selfname is ", po["selfname"]); |
|---|
| 115 |
|
|---|
| 116 |
po.disconnect; |
|---|
| 117 |
|
|---|
| 118 |
} catch (ProgramOptionsException e) { |
|---|
| 119 |
writefln(e.toString); |
|---|
| 120 |
return 1; |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|