Changeset 10
- Timestamp:
- 06/29/07 16:41:33 (2 years ago)
- Files:
-
- trunk/doost/any.d (modified) (2 diffs)
- trunk/doost/common.d (modified) (1 diff)
- trunk/doost/environment.d (modified) (2 diffs)
- trunk/doost/program_options/command_line_parser.d (modified) (15 diffs)
- trunk/doost/program_options/config_file_parser.d (modified) (7 diffs)
- trunk/doost/program_options/environment_parser.d (modified) (4 diffs)
- trunk/doost/program_options/errors.d (modified) (15 diffs)
- trunk/doost/program_options/libversion.d (modified) (1 diff)
- trunk/doost/program_options/option.d (modified) (3 diffs)
- trunk/doost/program_options/options_description.d (modified) (21 diffs)
- trunk/doost/program_options/positional_options.d (modified) (3 diffs)
- trunk/doost/program_options/string2type.d (modified) (1 diff)
- trunk/doost/program_options/value_semantic.d (modified) (15 diffs)
- trunk/doost/program_options/variables_map.d (modified) (7 diffs)
- trunk/examples/program_options/custom_syntax.d (modified) (3 diffs)
- trunk/examples/program_options/first.d (modified) (1 diff)
- trunk/examples/program_options/option_groups.d (modified) (4 diffs)
- trunk/examples/program_options/optionsdescription.d (modified) (3 diffs)
- trunk/examples/program_options/program_options.cbp (modified) (1 diff)
- trunk/examples/program_options/real.d (modified) (3 diffs)
- trunk/examples/program_options/regex.d (modified) (3 diffs)
- trunk/examples/program_options/response_file.d (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/doost/any.d
r8 r10 110 110 111 111 class Test { 112 stringtoString() {112 char[] toString() { 113 113 return "Test class"; 114 114 } … … 132 132 133 133 v="string test"[]; 134 assert(v.as!( string) == "string test");134 assert(v.as!(char[]) == "string test"); 135 135 136 136 v=&t.doTest; trunk/doost/common.d
r8 r10 10 10 11 11 public import std.stdio; 12 13 alias char[] string;trunk/doost/environment.d
r5 r10 24 24 25 25 class EnvException : Exception { 26 this( stringe) {26 this(char[] e) { 27 27 super(e); 28 28 } 29 29 } 30 30 31 string[string] getEnv() {32 string[string] result;31 char[][char[]] getEnv() { 32 char[][char[]] result; 33 33 char** m_environment=environ; 34 strings, key, value;34 char[] s, key, value; 35 35 uint n; 36 36 … … 49 49 } 50 50 51 string getEnvVariable(stringname) {51 char[] getEnvVariable(char[] name) { 52 52 return ""; 53 53 } 54 54 55 void setEnvVariable( string name, stringvalue) {55 void setEnvVariable(char[] name, char[] value) { 56 56 } 57 57 58 void removeEnvVariable( stringname) {58 void removeEnvVariable(char[] name) { 59 59 } trunk/doost/program_options/command_line_parser.d
r9 r10 106 106 /** Return type of aditional parser. */ 107 107 struct OptionPair { 108 stringname;109 stringvalue;108 char[] name; 109 char[] value; 110 110 } 111 111 112 alias OptionPair delegate( string) additional_parser;113 alias Option[] delegate(inout string[]) style_parser;112 alias OptionPair delegate(char[]) additional_parser; 113 alias Option[] delegate(inout char[][]) style_parser; 114 114 115 115 //------------------------------------------------------------------------------ … … 152 152 assumed to have optional parameter. 153 153 */ 154 this( string[] args) {154 this(char[][] args) { 155 155 m_args = args; 156 156 m_style = style_t.default_style; … … 214 214 215 215 216 stringself;216 char[] self; 217 217 if (m_style & style_t.first_is_self) { 218 218 self=m_args[0]; … … 234 234 // writefln("Before check ..."); 235 235 if (next.length!=0) { 236 string[] e;236 char[][] e; 237 237 for (uint k = 0; k < next.length-1; ++k) { 238 238 finishOption(next[k], e); … … 306 306 } 307 307 308 Option[] parseLongOption(inout string[] args) {308 Option[] parseLongOption(inout char[][] args) { 309 309 // writefln("parse_long_option: args.length=", args.length); 310 310 Option[] result; 311 stringtok = args[0];311 char[] tok = args[0]; 312 312 //writefln("parse_long_option::tok=", tok); 313 313 if (tok.length >= 3 && tok[0] == '-' && tok[1] == '-') { 314 stringname, adjacent;314 char[] name, adjacent; 315 315 316 316 int p = find(tok, '='); … … 336 336 337 337 338 Option[] parseShortOption(inout string[] args) {338 Option[] parseShortOption(inout char[][] args) { 339 339 // writefln("parse_short_option: args.length=", args.length); 340 340 Option[] result; 341 stringtok = args[0];341 char[] tok = args[0]; 342 342 //writefln("parse_short_option::tok=", tok); 343 343 344 344 if (tok.length >= 2 && tok[0] == '-' && tok[1] != '-') { 345 345 346 stringname = tok[0..2];347 stringadjacent = tok[2..$];346 char[] name = tok[0..2]; 347 char[] adjacent = tok[2..$]; 348 348 349 349 //writefln("parse_short_option::name=", name); … … 393 393 } 394 394 395 Option[] parseDosOption(inout string[] args) {395 Option[] parseDosOption(inout char[][] args) { 396 396 // writefln("parse_dos_option: args.length=", args.length); 397 397 Option[] result; 398 stringtok = args[0];398 char[] tok = args[0]; 399 399 if (tok.length >= 2 && tok[0] == '/') { 400 stringname = "-" ~ tok[1];401 stringadjacent = tok[2..$];400 char[] name = "-" ~ tok[1]; 401 char[] adjacent = tok[2..$]; 402 402 403 403 auto opt = new Option; … … 411 411 } 412 412 413 Option[] parseDisguisedLongOption(inout string[] args) {413 Option[] parseDisguisedLongOption(inout char[][] args) { 414 414 // writefln("parse_disguised_long_option: args.length=", args.length); 415 415 Option[] result; 416 stringtok = args[0];416 char[] tok = args[0]; 417 417 if (tok.length >= 2 && ((tok[0] == '-' && tok[1] != '-') || ((m_style & style_t.allow_slash_for_short) && tok[0] == '/'))) { 418 418 bool aprox=(m_style & style_t.allow_guessing)==0 ? false : true; … … 426 426 } 427 427 428 Option[] parseTerminator(inout string[] args) {428 Option[] parseTerminator(inout char[][] args) { 429 429 // writefln("parse_terminator: args.length=", args.length); 430 430 Option[] result; 431 stringtok = args[0];431 char[] tok = args[0]; 432 432 if (tok == "--") { 433 433 for (uint i = 1; i < args.length; ++i) { … … 441 441 } 442 442 443 Option[] handleAdditionalParser(inout string[] args) {443 Option[] handleAdditionalParser(inout char[][] args) { 444 444 // writefln("handle_additional_parser: args.length=", args.length); 445 445 Option[] result; … … 477 477 bool allow_some_long = (style & style_t.allow_long) || (style & style_t.allow_long_disguise); 478 478 479 stringerror;479 char[] error; 480 480 if (allow_some_long && !(style & style_t.long_allow_adjacent) && !(style & style_t.long_allow_next)) 481 481 error = "style disallows parameters for long options"; … … 493 493 } 494 494 495 void finishOption(Option opt, inout string[] other_tokens) {495 void finishOption(Option opt, inout char[][] other_tokens) { 496 496 if (opt.string_key.length==0) return; 497 497 … … 544 544 545 545 // Copies of input. 546 string[] m_args;546 char[][] m_args; 547 547 style_t m_style; 548 548 bool m_allow_unregistered; 549 stringm_self;549 char[] m_self; 550 550 551 551 OptionsDescription m_desc; … … 600 600 //TODO: Program name should be always added to variable map as default, when 601 601 //parsing command line 602 this( string[] args) {602 this(char[][] args) { 603 603 super(args); 604 604 } … … 647 647 */ 648 648 649 ParsedOptions parseCommandLine( string[] args, OptionsDescription desc, int style = 0, additional_parser ext = null) {649 ParsedOptions parseCommandLine(char[][] args, OptionsDescription desc, int style = 0, additional_parser ext = null) { 650 650 return (new CommandLineParser(args)).options(desc).style(style).extraParser(ext).run(); 651 651 } trunk/doost/program_options/config_file_parser.d
r9 r10 25 25 /** Parse a config file. 26 26 */ 27 ParsedOptions parseConfigFile( stringstr, OptionsDescription desc) {27 ParsedOptions parseConfigFile(char[] str, OptionsDescription desc) { 28 28 auto parser=new CommonConfigFileParser(desc); 29 29 return parser.parseFile(str); 30 30 } 31 31 32 ParsedOptions parseConfigFile( stringstr, OptionsDescription desc, CommonConfigFileParser parser) {32 ParsedOptions parseConfigFile(char[] str, OptionsDescription desc, CommonConfigFileParser parser) { 33 33 return parser.parseFile(str); 34 34 } … … 94 94 } 95 95 96 Option get( strings) {96 Option get(char[] s) { 97 97 uint n; 98 98 Option result; … … 110 110 m_prefix ~= '.'; 111 111 } else if ((n = find(s, '=')) != -1) { 112 stringname = m_prefix ~ strip(s[0..n]);113 stringvalue = strip(s[n+1..$]);112 char[] name = m_prefix ~ strip(s[0..n]); 113 char[] value = strip(s[n+1..$]); 114 114 115 115 if (!allowedOption(name)) … … 130 130 } 131 131 132 ParsedOptions parseFile( stringstr) {132 ParsedOptions parseFile(char[] str) { 133 133 auto result = new ParsedOptions(m_desc); 134 string[] lines=splitlines(str);134 char[][] lines=splitlines(str); 135 135 Option r; 136 136 … … 149 149 'foo_bar' are allowed. */ 150 150 151 void addOption( stringname) {152 strings=name;151 void addOption(char[] name) { 152 char[] s=name; 153 153 assert(s.length!=0); 154 154 if (s[$-1] == '*') { … … 181 181 182 182 // Returns true if 's' is a registered option name. 183 bool allowedOption( strings) {183 bool allowedOption(char[] s) { 184 184 if (s in allowed_options) 185 185 return true; … … 204 204 } 205 205 206 bool[ string] allowed_options;206 bool[char[]] allowed_options; 207 207 208 208 // Invariant: no element is prefix of other element. 209 bool[ string] allowed_prefixes;210 stringm_prefix;209 bool[char[]] allowed_prefixes; 210 char[] m_prefix; 211 211 OptionsDescription m_desc; 212 212 } trunk/doost/program_options/environment_parser.d
r9 r10 48 48 class PrefixNameMapper { 49 49 public: 50 this( stringprefix) {50 this(char[] prefix) { 51 51 this.prefix=prefix; 52 52 } 53 53 54 string opCall(strings) {55 stringresult;54 char[] opCall(char[] s) { 55 char[] result; 56 56 if (find(s, prefix) == 0) { 57 57 result=tolower(s[prefix.length..$]); … … 60 60 } 61 61 private: 62 stringprefix;62 char[] prefix; 63 63 } 64 64 … … 74 74 different from the naming of command line options. 75 75 */ 76 ParsedOptions parseEnvironment(OptionsDescription desc, string delegate (string) name_mapper) {76 ParsedOptions parseEnvironment(OptionsDescription desc, char[] delegate (char[]) name_mapper) { 77 77 auto result = new ParsedOptions(desc); 78 78 79 string[string] env=getEnv();79 char[][char[]] env=getEnv(); 80 80 81 81 foreach(key; env.keys) { 82 stringoption_name = name_mapper(key);82 char[] option_name = name_mapper(key); 83 83 84 84 if (option_name.length!=0) { … … 101 101 converting the remaining string into lower case. 102 102 */ 103 ParsedOptions parseEnvironment(OptionsDescription desc, stringprefix) {103 ParsedOptions parseEnvironment(OptionsDescription desc, char[] prefix) { 104 104 auto p=new PrefixNameMapper(prefix); 105 105 return parseEnvironment(desc, &p.opCall); trunk/doost/program_options/errors.d
r9 r10 19 19 class ProgramOptionsException : Exception { 20 20 public: 21 this( stringwhat) {21 this(char[] what) { 22 22 super(what); 23 23 } … … 29 29 class InvalidSyntax : ProgramOptionsException { 30 30 public: 31 this( string tokens, stringmsg) {31 this(char[] tokens, char[] msg) { 32 32 super(msg ~ " in '" ~ tokens ~ "'"); 33 33 this.tokens=tokens; 34 34 this.msg=msg; 35 35 } 36 stringtokens, msg;36 char[] tokens, msg; 37 37 } 38 38 … … 42 42 class UnknownOption : ProgramOptionsException { 43 43 public: 44 this( stringname) {44 this(char[] name) { 45 45 super("unknown option " ~ name); 46 46 } … … 52 52 class AmbiguousOption : ProgramOptionsException { 53 53 public: 54 this( string name, string[] alternatives) {54 this(char[] name, char[][] alternatives) { 55 55 super("ambiguous options: " ~ name); 56 56 this.alternatives=alternatives; 57 57 } 58 string[] alternatives;58 char[][] alternatives; 59 59 } 60 60 … … 65 65 class MultipleValues : ProgramOptionsException { 66 66 public: 67 this( stringwhat) {67 this(char[] what) { 68 68 super(what); 69 69 } … … 77 77 class MultipleOccurrences : ProgramOptionsException { 78 78 public: 79 this( stringwhat) {79 this(char[] what) { 80 80 super(what); 81 81 } … … 87 87 class ValidationError : ProgramOptionsException { 88 88 public: 89 this( stringwhat) {90 super(what); 91 } 92 void setOptionName( stringoption_name) {89 this(char[] what) { 90 super(what); 91 } 92 void setOptionName(char[] option_name) { 93 93 m_option_name = option_name; 94 94 } 95 95 96 96 private: 97 stringm_message; // For on-demand formatting in 'what'98 stringm_option_name; // The name of the option which97 char[] m_message; // For on-demand formatting in 'what' 98 char[] m_option_name; // The name of the option which 99 99 // caused the exception. 100 100 // Zamiana na toString? 101 stringwhat() {101 char[] what() { 102 102 if (m_option_name!="") { 103 103 m_message = "in option '" ~ m_option_name ~ "': " ~ "logic error"; … … 112 112 class InvalidOptionValue : ValidationError { 113 113 public: 114 this( stringvalue) {114 this(char[] value) { 115 115 super("invalid option value '" ~ value ~ "'"); 116 116 } … … 122 122 class TooManyPositionalOptionsError : ProgramOptionsException { 123 123 public: 124 this( stringwhat) {124 this(char[] what) { 125 125 super(what); 126 126 } … … 132 132 class TooFewPositionalOptionsError : ProgramOptionsException { 133 133 public: 134 this( stringwhat) {134 this(char[] what) { 135 135 super(what); 136 136 } … … 150 150 } 151 151 152 this( stringtokens, kind_t kind) {152 this(char[] tokens, kind_t kind) { 153 153 super(tokens, errorMessage(kind)); 154 154 m_kind=kind; … … 159 159 160 160 protected: 161 static stringerrorMessage(kind_t kind) {161 static char[] errorMessage(kind_t kind) { 162 162 // Initially, store the message in 'const char*' variable, 163 163 // to avoid conversion to std::string in all cases. 164 stringmsg;164 char[] msg; 165 165 switch(kind) { 166 166 case kind_t.long_not_allowed: … … 197 197 class InvalidConfigFileSyntax : InvalidSyntax { 198 198 public: 199 this( string tokens, stringmsg) {199 this(char[] tokens, char[] msg) { 200 200 super(tokens, msg); 201 201 } … … 206 206 class InvalidCommandLineStyle : ProgramOptionsException { 207 207 public: 208 this( stringmsg) {208 this(char[] msg) { 209 209 super(msg); 210 210 } … … 216 216 class DuplicateOptionError : ProgramOptionsException { 217 217 public: 218 this( stringwhat) {219 super(what); 220 } 221 } 218 this(char[] what) { 219 super(what); 220 } 221 } trunk/doost/program_options/libversion.d
r5 r10 15 15 import doost.common; 16 16 17 const stringprogram_options_version="2";18 const stringprogram_options_date="2006-12-02";17 const char[] program_options_version="2"; 18 const char[] program_options_date="2006-12-02"; trunk/doost/program_options/option.d
r5 r10 31 31 } 32 32 33 this( string string_key, string[] value) {33 this(char[] string_key, char[][] value) { 34 34 this.string_key=string_key; 35 35 this.value=value; … … 39 39 /** String key of this option. Intentionally independent of the template 40 40 parameter. */ 41 stringstring_key;41 char[] string_key; 42 42 43 43 /** Position key of this option. All options without an explicit name are … … 50 50 51 51 /** Option's value */ 52 string[] value;52 char[][] value; 53 53 54 54 /** True if option was not recognized. In that case, trunk/doost/program_options/options_description.d
r9 r10 72 72 */ 73 73 74 this( stringname, ValueSemantic s) {74 this(char[] name, ValueSemantic s) { 75 75 m_value_semantic=s; 76 76 setName(name); … … 81 81 */ 82 82 83 this( string name, ValueSemantic s, stringdescription) {83 this(char[] name, ValueSemantic s, char[] description) { 84 84 m_description=description; 85 85 m_value_semantic=s; … … 89 89 /** Given 'option', specified in the input source, 90 90 return 'true' is 'option' specifies *this. */ 91 MatchResult match( stringoption, bool approx) {91 MatchResult match(char[] option, bool approx) { 92 92 MatchResult result = MatchResult.no_match; 93 93 //writefln("m_long_name=", m_long_name, " approx=", approx, " option=", option); … … 126 126 it's a short name with prepended '-'. 127 127 */ 128 string key(stringoption) {128 char[] key(char[] option) { 129 129 if (m_long_name!="") 130 130 if (find(m_long_name, "*") != -1) … … 141 141 } 142 142 143 stringlongName() {143 char[] longName() { 144 144 return m_long_name; 145 145 } 146 146 147 147 /// Explanation of this option 148 stringdescription() {148 char[] description() { 149 149 return m_description; 150 150 } … … 156 156 157 157 /// Returns the option name, formatted suitably for usage message. 158 stringformatName() {158 char[] formatName() { 159 159 if (m_short_name!="") return m_short_name~" [--"~m_long_name~"]"; 160 160 else return "--"~m_long_name; … … 163 163 /** Return the parameter name and properties, formatted suitably for 164 164 usage message. */ 165 stringformatParameter() {165 char[] formatParameter() { 166 166 if (m_value_semantic.maxTokens != 0) return m_value_semantic.name; 167 167 else return ""; … … 181 181 182 182 private: 183 OptionDescription setName( string_name) {184 stringname=_name;183 OptionDescription setName(char[] _name) { 184 char[] name=_name; 185 185 uint n = find(name, ','); 186 186 if (n != -1) { … … 195 195 } 196 196 197 stringm_short_name, m_long_name, m_description;197 char[] m_short_name, m_long_name, m_description; 198 198 ValueSemantic m_value_semantic; 199 199 } … … 212 212 // no value can be specified on command line. 213 213 // FIXME: does not look exception-safe 214 OptionsDescriptionEasyInit opCall( string name, stringdescription) {214 OptionsDescriptionEasyInit opCall(char[] name, char[] description) { 215 215 owner.add(new OptionDescription(name, new UntypedValue(true), description)); 216 216 return this; 217 217 } 218 218 219 OptionsDescriptionEasyInit opCall( stringname, ValueSemantic s) {219 OptionsDescriptionEasyInit opCall(char[] name, ValueSemantic s) { 220 220 owner.add(new OptionDescription(name, s)); 221 221 return this; 222 222 } 223 223 224 OptionsDescriptionEasyInit opCall( string name, ValueSemantic s, stringdescription) {224 OptionsDescriptionEasyInit opCall(char[] name, ValueSemantic s, char[] description) { 225 225 owner.add(new OptionDescription(name, s, description)); 226 226 return this; 227 227 } 228 228 229 OptionsDescriptionEasyInit opCall( stringselfdir, ValueSemantic s1,230 stringselfname, ValueSemantic s2) {229 OptionsDescriptionEasyInit opCall(char[] selfdir, ValueSemantic s1, 230 char[] selfname, ValueSemantic s2) { 231 231 232 232 auto sd=new OptionDescription(selfdir, s1); … … 265 265 /** Creates the instance. The 'caption' parameter gives the name of 266 266 this 'OptionsDescription' instance. Primarily useful for output. */ 267 this( stringcaption, uint line_length = m_default_line_length) {267 this(char[] caption, uint line_length = m_default_line_length) { 268 268 m_caption=caption; 269 269 m_line_length=line_length; … … 306 306 } 307 307 308 OptionDescription find( stringname, bool approx) {308 OptionDescription find(char[] name, bool approx) { 309 309 OptionDescription d = findNothrow(name, approx); 310 310 if (d is null) throw new UnknownOption(name); … … 312 312 } 313 313 314 OptionDescription findNothrow( stringname, bool approx) {314 OptionDescription findNothrow(char[] name, bool approx) { 315 315 int found = -1; 316 316 // We use linear search because matching specified option … … 337 337 338 338 if (found != -1) { 339 string[] alts;339 char[][] alts; 340 340 // FIXME: the use of 'key' here might not 341 341 // be the best approach. … … 363 363 OptionDescription element. */ 364 364 365 stringtoString() {366 stringresult;365 char[] toString() { 366 char[] result; 367 367 if (m_caption!="") result~=m_caption ~ ":\n"; 368 368 … … 372 372 foreach(opt; m_options) { 373 373 if (!opt.hidden) { 374 stringss;374 char[] ss; 375 375 ss = " " ~ opt.formatName() ~ ' ' ~ opt.formatParameter(); 376 376 width = max!(uint)(width, ss.length); … … 399 399 private: 400 400 401 stringm_caption;401 char[] m_caption; 402 402 uint m_line_length; 403 403 // Data organization is chosen because: … … 424 424 425 425 426 string formatParagraph(stringpar, uint indent, uint line_length) {427 stringresult;426 char[] formatParagraph(char[] par, uint indent, uint line_length) { 427 char[] result; 428 428 429 429 // Through reminder of this function, 'line_length' will … … 522 522 //------------------------------------------------------------------------------ 523 523 524 string formatDescription(stringdesc, uint first_column_width, uint line_length) {524 char[] formatDescription(char[] desc, uint first_column_width, uint line_length) { 525 525 // we need to use one char less per line to work correctly if actual 526 526 // console has longer lines 527 527 assert(line_length > 1); 528 stringresult;528 char[] result; 529 529 530 530 if (line_length > 1) --line_length; … … 561 561 // Uproszczony tokenizer 562 562 563 string[] lines=splitlines(desc);563 char[][] lines=splitlines(desc); 564 564 foreach(line; lines) { 565 565 result~=formatParagraph(line, first_column_width, line_length); … … 578 578 //------------------------------------------------------------------------------ 579 579 580 stringformatOne(OptionDescription opt, uint first_column_width, uint line_length) {581 stringresult=" " ~ opt.formatName() ~ ' ' ~ opt.formatParameter();580 char[] formatOne(OptionDescription opt, uint first_column_width, uint line_length) { 581 char[] result=" " ~ opt.formatName() ~ ' ' ~ opt.formatParameter(); 582 582 583 583 if (opt.description()!="") { trunk/doost/program_options/positional_options.d
r9 r10 41 41 '-1'. 42 42 */ 43 PositionalOptionsDescription add( stringname, int max_count) {43 PositionalOptionsDescription add(char[] name, int max_count) { 44 44 assert(max_count != -1 || m_trailing==""); 45 45 … … 63 63 Precondition: position < max_total_count() 64 64 */ 65 stringnameForPosition(uint position) {65 char[] nameForPosition(uint position) { 66 66 assert(position < maxTotalCount()); 67 67 … … 74 74 // positions is unlimited, then the last name is stored in 75 75 // m_trailing; 76 string[] m_names;77 stringm_trailing;76 char[][] m_names; 77 char[] m_trailing; 78 78 } trunk/doost/program_options/string2type.d
r5 r10 12 12 import doost.common; 13 13 14 T stringToType(T : string)(strings) {14 T stringToType(T : char[])(char[] s) { 15 15 return s; 16 16 } 17 17 18 T stringToType(T : int)( strings) {18 T stringToType(T : int)(char[] s) { 19 19 return toInt(s); 20 20 } 21 21 22 T stringToType(T : uint)( strings) {22 T stringToType(T : uint)(char[] s) { 23 23 return toUint(s); 24 24 } 25 25 26 T stringToType(T : long)( strings) {26 T stringToType(T : long)(char[] s) { 27 27 return toLong(s); 28 28 } 29 29 30 T stringToType(T : ulong)( strings) {30 T stringToType(T : ulong)(char[] s) { 31 31 return toUlong(s); 32 32 } 33 33 34 T stringToType(T : short)( strings) {34 T stringToType(T : short)(char[] s) { 35 35 return toShort(s); 36 36 } 37 37 38 T stringToType(T : ushort)( strings) {38 T stringToType(T : ushort)(char[] s) { 39 39 return toUshort(s); 40 40 } 41 41 42 T stringToType(T : byte)( strings) {42 T stringToType(T : byte)(char[] s) { 43 43 return toByte(s); 44 44 } 45 45 46 T stringToType(T : ubyte)( strings) {46 T stringToType(T : ubyte)(char[] s) { 47 47 return toUbyte(s); 48 48 } 49 49 50 T stringToType(T : float)( strings) {50 T stringToType(T : float)(char[] s) { 51 51 return toFloat(s); 52 52 } 53 53 54 T stringToType(T : double)( strings) {54 T stringToType(T : double)(char[] s) { 55 55 return toDouble(s); 56 56 } 57 57 58 T stringToType(T : real)( strings) {58 T stringToType(T : real)(char[] s) { 59 59 return toReal(s); 60 60 } trunk/doost/program_options/value_semantic.d
r9 r10 21 21 //------------------------------------------------------------------------------ 22 22 23 const stringarg="arg";23 const char[] arg="arg"; 24 24 25 25 //------------------------------------------------------------------------------ … … 32 32 for automatic help message. 33 33 */ 34 stringname();34 char[] name(); 35 35 36 36 /** The minimum number of tokens for this option that … … 53 53 option is specified more than once. 54 54 */ 55 void parse(Any value_store, string[] new_tokens);55 void parse(Any value_store, char[][] new_tokens); 56 56 57 57 /** Called to assign default value to 'value_store'. Returns … … 68 68 69 69 /** Class which specifies a simple handling of a value: the value will 70 have stringtype and only one token is allowed. */70 have char[] type and only one token is allowed. */ 71 71 class UntypedValue : ValueSemantic { 72 72 public: … … 75 75 } 76 76 77 override stringname() {77 override char[] name() { 78 78 return arg; 79 79 } … … 98 98 any modifications. 99 99 */ 100 override void parse(Any value_store, string[] new_tokens) {100 override void parse(Any value_store, char[][] new_tokens) { 101 101 if (!value_store.empty()) throw new MultipleOccurrences("multiple_occurrences"); 102 102 if (new_tokens.length > 1) throw new MultipleValues("multiple_values"); 103 103 104 value_store = (new_tokens.length == 0) ? (new Any()).assign(cast( string)("")) : (new Any).assign(new_tokens[0]);104 value_store = (new_tokens.length == 0) ? (new Any()).assign(cast(char[])("")) : (new Any).assign(new_tokens[0]); 105 105 } 106 106 … … 163 163 by the user. 164 164 */ 165 TypedValue defaultValue(T v, stringtextual) {165 TypedValue defaultValue(T v, char[] textual) { 166 166 m_default_value.assign(v); 167 167 m_default_value_as_text = textual; … … 197 197 // value semantic overrides 198 198 199 override stringname() {199 override char[] name() { 200 200 if (!m_default_value.empty() && m_default_value_as_text!="") { 201 201 return arg ~ " (=" ~ m_default_value_as_text ~ ")"; … … 226 226 /** Creates an instance of the 'validator' class and calls 227 227 its operator() to perform the actual conversion. */ 228 override void parse(Any value_store, string[] new_tokens) {228 override void parse(Any value_store, char[][] new_tokens) { 229 229 validate!(T)(value_store, new_tokens); 230 230 } … … 264 264 // as boost::optional to avoid unnecessary instantiations. 265 265 Any m_default_value; 266 stringm_default_value_as_text;266 char[] m_default_value_as_text; 267 267 bool m_composing, m_multitoken, m_zero_tokens; 268 268 void delegate(T) m_notifier; … … 304 304 otherwise. */ 305 305 306 string getSingleString(string[] v, bool allow_empty = false) {306 char[] getSingleString(char[][] v, bool allow_empty = false) { 307 307 if (v.length > 1) throw new ValidationError("multiple values not allowed"); 308 308 if (v.length == 1) return v[0]; … … 328 328 partial template ordering, just like the last 'long/int' parameter. 329 329 */ 330 void validate(T)(Any v, string[] xs) {330 void validate(T)(Any v, char[][] xs) { 331 331 checkFirstOccurrence(v); 332 strings=getSingleString(xs);332 char[] s=getSingleString(xs); 333 333 try { 334 334 //v = Any(lexical_cast<T>(s)); … … 344 344 /** Validates sequences. Allows multiple values per option occurrence 345 345 and multiple occurrences. */ 346 void validate(T : T[])(Any v, string[] s) {346 void validate(T : T[])(Any v, char[][] s) { 347 347 if (v.empty()) { 348 348 T[] t; … … 376 376 be optional. 377 377 */ 378 void validate(T : bool)(Any v, string[] xs) {378 void validate(T : bool)(Any v, char[][] xs) { 379 379 checkFirstOccurrence(v); 380 strings=tolower(getSingleString(xs, true));380 char[] s=tolower(getSingleString(xs, true)); 381 381 382 382 if (s.length==0 || s == "on" || s == "yes" || s == "1" || s == "true") … … 390 390 //------------------------------------------------------------------------------ 391 391 392 void validate(T : string)(Any v, string[] xs) {392 void validate(T : char[])(Any v, char[][] xs) { 393 393 checkFirstOccurrence(v); 394 strings=getSingleString(xs);394 char[] s=getSingleString(xs); 395 395 if ((s!="") && (s[0] == '\'' && s[$-1] == '\'' || s[0] == '"' && s[$-1] == '"')) 396 396 v.assign(s[1..$-2]);
