| 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 a user-defined class can be parsed using |
|---|
| 18 |
specific mechanism -- not the iostream operations used by default. |
|---|
| 19 |
|
|---|
| 20 |
A new class 'magic_number' is defined and the 'validate' method is overloaded |
|---|
| 21 |
to validate the values of that class using Boost.Regex. |
|---|
| 22 |
To test, run |
|---|
| 23 |
|
|---|
| 24 |
regex -m 123-456 |
|---|
| 25 |
regex -m 123-4567 |
|---|
| 26 |
|
|---|
| 27 |
The first invocation should output: |
|---|
| 28 |
|
|---|
| 29 |
The magic is "456" |
|---|
| 30 |
|
|---|
| 31 |
and the second invocation should issue an error message. |
|---|
| 32 |
|
|---|
| 33 |
******************************************************************************/ |
|---|
| 34 |
|
|---|
| 35 |
import std.stdio; |
|---|
| 36 |
|
|---|
| 37 |
import doost.util.config.ProgramOptions; |
|---|
| 38 |
import doost.util.config.CommandLineStorage; |
|---|
| 39 |
import doost.util.config.Converter; |
|---|
| 40 |
import doost.util.config.Value; |
|---|
| 41 |
|
|---|
| 42 |
//------------------------------------------------------------------------------ |
|---|
| 43 |
|
|---|
| 44 |
/******************************************************************************* |
|---|
| 45 |
Completely non-sensical class just for usage in example. |
|---|
| 46 |
******************************************************************************/ |
|---|
| 47 |
class MagicNumber { |
|---|
| 48 |
public: |
|---|
| 49 |
this(int first, int second) { |
|---|
| 50 |
this.first = first; |
|---|
| 51 |
this.second = second; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
int first; |
|---|
| 55 |
int second; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
/******************************************************************************* |
|---|
| 59 |
Overload the 'parseValue' function for the user-defined class. |
|---|
| 60 |
It makes sure that value is of form XXX-XXX where X are digits and converts |
|---|
| 61 |
the second group to an integer. This has no practical meaning, meant only |
|---|
| 62 |
to show how regex can be used to validate values. |
|---|
| 63 |
******************************************************************************/ |
|---|
| 64 |
MagicNumber parseValue(TypedValue!(MagicNumber) owner, char[] str) { |
|---|
| 65 |
string s = str.dup; |
|---|
| 66 |
string pattern=r"\d\d\d-\d\d\d"; |
|---|
| 67 |
|
|---|
| 68 |
std.regexp.RegExp re = std.regexp.search(s, pattern); |
|---|
| 69 |
if (re is null) |
|---|
| 70 |
throw new InvalidOptionValueException(s, pattern); |
|---|
| 71 |
if (re.match(0) != s) |
|---|
| 72 |
throw new InvalidOptionValueException(s, pattern); |
|---|
| 73 |
|
|---|
| 74 |
return new MagicNumber(to!(int)(str[0..3]), to!(int)(str[4..$])); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
/******************************************************************************* |
|---|
| 78 |
Program entry point |
|---|
| 79 |
******************************************************************************/ |
|---|
| 80 |
void main(char[][] args) { |
|---|
| 81 |
try { |
|---|
| 82 |
auto desc= new RegularOptions("Allowed options"); |
|---|
| 83 |
desc.options() |
|---|
| 84 |
("help", "produce a help screen") |
|---|
| 85 |
("version,v", "print the version number") |
|---|
| 86 |
("magic,m", define!(MagicNumber)(r"\d\d\d-\d\d\d").parser(&parseValue), "magic value (in NNN-NNN format)") |
|---|
| 87 |
; |
|---|
| 88 |
|
|---|
| 89 |
auto po = (new ProgramOptions).next( |
|---|
| 90 |
(new CommandLineStorage(args)) |
|---|
| 91 |
.options(desc) |
|---|
| 92 |
); |
|---|
| 93 |
|
|---|
| 94 |
po.connect; |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
if ("help" in po) { |
|---|
| 98 |
writefln("Usage: regex [options]"); |
|---|
| 99 |
writefln(desc); |
|---|
| 100 |
return 0; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
if ("version" in po) { |
|---|
| 104 |
writefln("Version 1."); |
|---|
| 105 |
return 0; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
if ("magic" in po) { |
|---|
| 109 |
writefln("The magic is \"", po["magic"].as!(MagicNumber)().first, "\" and \"", po["magic"].as!(MagicNumber)().second, "\""); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
po.disconnect; |
|---|
| 113 |
} |
|---|
| 114 |
catch(ProgramOptionsException e) { |
|---|
| 115 |
writefln(e); |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|