Changeset 9
- Timestamp:
- 02/25/07 16:45:56 (2 years ago)
- Files:
-
- trunk/doost/program_options/command_line_parser.d (modified) (20 diffs)
- trunk/doost/program_options/config_file_parser.d (modified) (8 diffs)
- trunk/doost/program_options/environment_parser.d (modified) (2 diffs)
- trunk/doost/program_options/errors.d (modified) (3 diffs)
- trunk/doost/program_options/options_description.d (modified) (21 diffs)
- trunk/doost/program_options/positional_options.d (modified) (2 diffs)
- trunk/doost/program_options/value_semantic.d (modified) (18 diffs)
- trunk/doost/program_options/variables_map.d (modified) (7 diffs)
- trunk/examples/program_options/custom_syntax.d (modified) (2 diffs)
- trunk/examples/program_options/first.d (modified) (2 diffs)
- trunk/examples/program_options/multiple_sources.d (modified) (4 diffs)
- trunk/examples/program_options/option_groups.d (modified) (3 diffs)
- trunk/examples/program_options/optionsdescription.d (modified) (1 diff)
- trunk/examples/program_options/program_options.cbp (modified) (3 diffs)
- trunk/examples/program_options/real.d (modified) (2 diffs)
- trunk/examples/program_options/regex.d (modified) (5 diffs)
- trunk/examples/program_options/response_file.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/doost/program_options/command_line_parser.d
r5 r9 164 164 if (style == 0) style = style_t.default_style; 165 165 166 check _style(style);166 checkStyle(style); 167 167 m_style = cast(style_t)(style); 168 168 } 169 169 170 void allow _unregistered() {170 void allowUnregistered() { 171 171 m_allow_unregistered = true; 172 172 } 173 173 174 void set _options_description(OptionsDescription desc) {174 void setOptionsDescription(OptionsDescription desc) { 175 175 m_desc = desc; 176 176 } 177 177 178 178 179 void set _positional_options(PositionalOptionsDescription positional) {179 void setPositionalOptions(PositionalOptionsDescription positional) { 180 180 m_positional = positional; 181 181 } … … 199 199 200 200 if (m_style_parser) style_parsers~=m_style_parser; 201 if (m_additional_parser) style_parsers~=&handle _additional_parser;202 if (m_style & style_t.allow_long) style_parsers~=&parse _long_option;203 if ((m_style & style_t.allow_long_disguise)) style_parsers~=&parse _disguised_long_option;204 if ((m_style & style_t.allow_short) && (m_style & style_t.allow_dash_for_short)) style_parsers~=&parse _short_option;205 if ((m_style & style_t.allow_short) && (m_style & style_t.allow_slash_for_short)) style_parsers~=&parse _dos_option;206 207 style_parsers~=&parse _terminator;201 if (m_additional_parser) style_parsers~=&handleAdditionalParser; 202 if (m_style & style_t.allow_long) style_parsers~=&parseLongOption; 203 if ((m_style & style_t.allow_long_disguise)) style_parsers~=&parseDisguisedLongOption; 204 if ((m_style & style_t.allow_short) && (m_style & style_t.allow_dash_for_short)) style_parsers~=&parseShortOption; 205 if ((m_style & style_t.allow_short) && (m_style & style_t.allow_slash_for_short)) style_parsers~=&parseDosOption; 206 207 style_parsers~=&parseTerminator; 208 208 209 209 Option[] result; … … 236 236 string[] e; 237 237 for (uint k = 0; k < next.length-1; ++k) { 238 finish _option(next[k], e);238 finishOption(next[k], e); 239 239 } 240 240 // For the last option, pass the unparsed tokens 241 241 // so that they can be added to next.back()'s values 242 242 // if appropriate. 243 finish _option(next[$-1], m_args);243 finishOption(next[$-1], m_args); 244 244 for (uint j = 0; j < next.length; ++j) 245 245 result~=next[j]; … … 283 283 //writefln("opt.position_key: ", opt.position_key); 284 284 if (opt.position_key != -1) { 285 if (position >= m_positional.max _total_count()) {285 if (position >= m_positional.maxTotalCount()) { 286 286 throw new TooManyPositionalOptionsError("too many positional options"); 287 287 } 288 opt.string_key = m_positional.name _for_position(position);288 opt.string_key = m_positional.nameForPosition(position); 289 289 //writefln("Name for position ", position, "; ", m_positional.name_for_position(position)); 290 290 ++position; … … 296 296 foreach(od; m_desc.options()) { 297 297 if (od.is_selfdir) 298 result~=new Option(od.long _name, [getDirName(self)]);298 result~=new Option(od.longName, [getDirName(self)]); 299 299 300 300 if (od.is_selfname) 301 result~=new Option(od.long _name, [getBaseName(self)]);302 } 303 } 304 305 return result; 306 } 307 308 Option[] parse _long_option(inout string[] args) {301 result~=new Option(od.longName, [getBaseName(self)]); 302 } 303 } 304 305 return result; 306 } 307 308 Option[] parseLongOption(inout string[] args) { 309 309 // writefln("parse_long_option: args.length=", args.length); 310 310 Option[] result; … … 336 336 337 337 338 Option[] parse _short_option(inout string[] args) {338 Option[] parseShortOption(inout string[] args) { 339 339 // writefln("parse_short_option: args.length=", args.length); 340 340 Option[] result; … … 358 358 for (;;) { 359 359 // writefln("m_desc is null: ", m_desc is null); 360 OptionDescription d = m_desc.find _nothrow(name, false);360 OptionDescription d = m_desc.findNothrow(name, false); 361 361 // writefln("parse_short_option::d=", d is null); 362 362 363 363 // FIXME: check for 'allow_sticky'. 364 364 if (d !is null && (m_style & style_t.allow_sticky) && 365 d.semantic ().max_tokens() == 0 && !adjacent.length==0) {365 d.semantic.maxTokens() == 0 && !adjacent.length==0) { 366 366 // 'adjacent' is in fact further option. 367 367 // writefln("parse_short_option:: in 'if' condition"); … … 393 393 } 394 394 395 Option[] parse _dos_option(inout string[] args) {395 Option[] parseDosOption(inout string[] args) { 396 396 // writefln("parse_dos_option: args.length=", args.length); 397 397 Option[] result; … … 411 411 } 412 412 413 Option[] parse _disguised_long_option(inout string[] args) {413 Option[] parseDisguisedLongOption(inout string[] args) { 414 414 // writefln("parse_disguised_long_option: args.length=", args.length); 415 415 Option[] result; … … 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; 419 if (m_desc.find _nothrow(tok[1..find(tok, '=')-1], aprox)) {419 if (m_desc.findNothrow(tok[1..find(tok, '=')-1], aprox)) { 420 420 args[0]="-"~args[0]; 421 421 if (args[0][1] == '/') args[0][1] = '-'; 422 return parse _long_option(args);423 } 424 } 425 return result; 426 } 427 428 Option[] parse _terminator(inout string[] args) {422 return parseLongOption(args); 423 } 424 } 425 return result; 426 } 427 428 Option[] parseTerminator(inout string[] args) { 429 429 // writefln("parse_terminator: args.length=", args.length); 430 430 Option[] result; … … 441 441 } 442 442 443 Option[] handle _additional_parser(inout string[] args) {443 Option[] handleAdditionalParser(inout string[] args) { 444 444 // writefln("handle_additional_parser: args.length=", args.length); 445 445 Option[] result; … … 465 465 Note that additional parser can match only one token. 466 466 */ 467 void set _additional_parser(additional_parser p) {467 void setAdditionalParser(additional_parser p) { 468 468 m_additional_parser = p; 469 469 } 470 470 471 void extra _style_parser(style_parser s) {471 void extraStyleParser(style_parser s) { 472 472 m_style_parser = s; 473 473 } 474 474 475 475 476 void check _style(int style) {476 void checkStyle(int style) { 477 477 bool allow_some_long = (style & style_t.allow_long) || (style & style_t.allow_long_disguise); 478 478 … … 493 493 } 494 494 495 void finish _option(Option opt, inout string[] other_tokens) {495 void finishOption(Option opt, inout string[] other_tokens) { 496 496 if (opt.string_key.length==0) return; 497 497 … … 500 500 bool aprox=(m_style & style_t.allow_guessing)==0 ? false : true; 501 501 //writefln("m_style & style_t.allow_guessing=", cast(int)(m_style & style_t.allow_guessing), " ", cast(int)m_style, " ", cast(int)(style_t.allow_guessing)); 502 OptionDescription xd = m_desc.find _nothrow(opt.string_key, aprox);502 OptionDescription xd = m_desc.findNothrow(opt.string_key, aprox); 503 503 504 504 if (!xd) { … … 521 521 // We don't check if those tokens look like option, or not! 522 522 523 uint min_tokens = d.semantic ().min_tokens();524 uint max_tokens = d.semantic ().max_tokens();523 uint min_tokens = d.semantic.minTokens; 524 uint max_tokens = d.semantic.maxTokens; 525 525 526 526 uint present_tokens = opt.value.length + other_tokens.length; … … 589 589 nuber of parameters will be confusing. 590 590 591 For the most common case, the function parse _command_line is a better591 For the most common case, the function parseCommandLine is a better 592 592 alternative. 593 593 */ … … 607 607 /** Sets options descriptions to use. */ 608 608 CommandLineParser options(OptionsDescription desc) { 609 set _options_description(desc);609 setOptionsDescription(desc); 610 610 m_desc = desc; 611 611 return this; … … 614 614 /** Sets positional options description to use. */ 615 615 CommandLineParser positional(PositionalOptionsDescription desc) { 616 set _positional_options(desc);616 setPositionalOptions(desc); 617 617 return this; 618 618 } … … 626 626 627 627 /** Sets the extra parsers. */ 628 CommandLineParser extra _parser(additional_parser ext) {629 set _additional_parser(ext);628 CommandLineParser extraParser(additional_parser ext) { 629 setAdditionalParser(ext); 630 630 return this; 631 631 } … … 647 647 */ 648 648 649 ParsedOptions parse _command_line(string[] args, OptionsDescription desc, int style = 0, additional_parser ext = null) {650 return (new CommandLineParser(args)).options(desc).style(style).extra _parser(ext).run();649 ParsedOptions parseCommandLine(string[] args, OptionsDescription desc, int style = 0, additional_parser ext = null) { 650 return (new CommandLineParser(args)).options(desc).style(style).extraParser(ext).run(); 651 651 } 652 652 trunk/doost/program_options/config_file_parser.d
r5 r9 21 21 import doost.program_options.options_description; 22 22 23 //------------------------------------------------------------------------------ 24 23 25 /** Parse a config file. 24 26 */ 25 ParsedOptions parse _config_file(string str, OptionsDescription desc) {27 ParsedOptions parseConfigFile(string str, OptionsDescription desc) { 26 28 auto parser=new CommonConfigFileParser(desc); 27 return parser.parse _file(str);28 } 29 30 ParsedOptions parse _config_file(string str, OptionsDescription desc, CommonConfigFileParser parser) {31 return parser.parse _file(str);29 return parser.parseFile(str); 30 } 31 32 ParsedOptions parseConfigFile(string str, OptionsDescription desc, CommonConfigFileParser parser) { 33 return parser.parseFile(str); 32 34 } 33 35 … … 38 40 39 41 /* 40 ParsedOptions parse _config_file(std::istream& is) {42 ParsedOptions parseConfigFile(std::istream& is) { 41 43 detail::config_file_iterator cf(is, false); 42 44 ParsedOptions result(0); … … 85 87 OptionDescription[] options = desc.options(); 86 88 foreach(o; options) { 87 if (o.long _name().length==0)89 if (o.longName.length==0) 88 90 throw new InvalidConfigFileSyntax("", "long name required for config file"); 89 allowed_options[o.long _name()]=true;90 add _option(o.long_name());91 allowed_options[o.longName()]=true; 92 addOption(o.longName()); 91 93 } 92 94 } … … 104 106 // Handle section name 105 107 if (s[0] == '[' && s[$-1] == ']') { 106 m_prefix = s[1..$- 2];108 m_prefix = s[1..$-1]; 107 109 if (m_prefix[$-1] != '.') 108 110 m_prefix ~= '.'; … … 111 113 string value = strip(s[n+1..$]); 112 114 113 if (!allowed _option(name))115 if (!allowedOption(name)) 114 116 throw new UnknownOption(name); 115 117 … … 128 130 } 129 131 130 ParsedOptions parse _file(string str) {132 ParsedOptions parseFile(string str) { 131 133 auto result = new ParsedOptions(m_desc); 132 134 string[] lines=splitlines(str); … … 147 149 'foo_bar' are allowed. */ 148 150 149 void add _option(string name) {151 void addOption(string name) { 150 152 string s=name; 151 153 assert(s.length!=0); … … 179 181 180 182 // Returns true if 's' is a registered option name. 181 bool allowed _option(string s) {183 bool allowedOption(string s) { 182 184 if (s in allowed_options) 183 185 return true; trunk/doost/program_options/environment_parser.d
r5 r9 74 74 different from the naming of command line options. 75 75 */ 76 ParsedOptions parse _environment(OptionsDescription desc, string delegate (string) name_mapper) {76 ParsedOptions parseEnvironment(OptionsDescription desc, string delegate (string) name_mapper) { 77 77 auto result = new ParsedOptions(desc); 78 78 … … 101 101 converting the remaining string into lower case. 102 102 */ 103 ParsedOptions parse _environment(OptionsDescription desc, string prefix) {103 ParsedOptions parseEnvironment(OptionsDescription desc, string prefix) { 104 104 auto p=new PrefixNameMapper(prefix); 105 return parse _environment(desc, &p.opCall);105 return parseEnvironment(desc, &p.opCall); 106 106 } trunk/doost/program_options/errors.d
r5 r9 90 90 super(what); 91 91 } 92 void set _option_name(string option_name) {92 void setOptionName(string option_name) { 93 93 m_option_name = option_name; 94 94 } … … 151 151 152 152 this(string tokens, kind_t kind) { 153 super(tokens, error _message(kind));153 super(tokens, errorMessage(kind)); 154 154 m_kind=kind; 155 155 } … … 159 159 160 160 protected: 161 static string error _message(kind_t kind) {161 static string 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. trunk/doost/program_options/options_description.d
r5 r9 39 39 */ 40 40 41 enum match_result { no_match, full_match, approximate_match };41 enum MatchResult { no_match, full_match, approximate_match }; 42 42 43 43 class OptionDescription { … … 53 53 create objects of types derived from 'ValueSemantic': 54 54 OptionsDescription d; 55 d.add _options()("a", parameter<int>("n")->default_value(1));55 d.addOptions()("a", parameter<int>("n")->default_value(1)); 56 56 Here, the static type returned by 'parameter' should be derived 57 57 from ValueSemantic. … … 74 74 this(string name, ValueSemantic s) { 75 75 m_value_semantic=s; 76 set _name(name);76 setName(name); 77 77 }; 78 78 … … 84 84 m_description=description; 85 85 m_value_semantic=s; 86 set _name(name);86 setName(name); 87 87 }; 88 88 89 89 /** Given 'option', specified in the input source, 90 90 return 'true' is 'option' specifies *this. */ 91 match_result match(string option, bool approx) {92 match_result result = match_result.no_match;91 MatchResult match(string option, bool approx) { 92 MatchResult result = MatchResult.no_match; 93 93 //writefln("m_long_name=", m_long_name, " approx=", approx, " option=", option); 94 94 if (m_long_name!="") { … … 97 97 // prefix is OK. 98 98 if (find(option, m_long_name[0..$-2])==0) 99 result= match_result.approximate_match;99 result=MatchResult.approximate_match; 100 100 } 101 101 … … 103 103 if (find(m_long_name, option) == 0) 104 104 if (m_long_name == option) 105 result = match_result.full_match;105 result = MatchResult.full_match; 106 106 else 107 result = match_result.approximate_match;107 result = MatchResult.approximate_match; 108 108 } else { 109 109 if (m_long_name == option) 110 result = match_result.full_match;110 result = MatchResult.full_match; 111 111 } 112 112 } 113 113 114 114 if (m_short_name == option) 115 result = match_result.full_match;115 result = MatchResult.full_match; 116 116 117 117 return result; … … 141 141 } 142 142 143 string long _name() {143 string longName() { 144 144 return m_long_name; 145 145 } … … 156 156 157 157 /// Returns the option name, formatted suitably for usage message. 158 string format _name() {158 string 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 string format _parameter() {166 if (m_value_semantic.max _tokens() != 0) return m_value_semantic.name();165 string formatParameter() { 166 if (m_value_semantic.maxTokens != 0) return m_value_semantic.name; 167 167 else return ""; 168 168 } … … 181 181 182 182 private: 183 OptionDescription set _name(string _name) {183 OptionDescription setName(string _name) { 184 184 string name=_name; 185 185 uint n = find(name, ','); … … 248 248 249 249 /** A set of option descriptions. This provides convenient interface for 250 adding new option (the add _options) method, and facilities to search250 adding new option (the addOptions) method, and facilities to search 251 251 for options by name. 252 252 … … 302 302 new OptionDescription instance and add it. 303 303 */ 304 OptionsDescriptionEasyInit add _options() {304 OptionsDescriptionEasyInit addOptions() { 305 305 return new OptionsDescriptionEasyInit(this); 306 306 } 307 307 308 308 OptionDescription find(string name, bool approx) { 309 OptionDescription d = find _nothrow(name, approx);309 OptionDescription d = findNothrow(name, approx); 310 310 if (d is null) throw new UnknownOption(name); 311 311 return d; 312 312 } 313 313 314 OptionDescription find _nothrow(string name, bool approx) {314 OptionDescription findNothrow(string name, bool approx) { 315 315 int found = -1; 316 316 // We use linear search because matching specified option … … 319 319 for (uint i = 0; i < m_options.length; ++i) { 320 320 321 match_result r = m_options[i].match(name, approx);322 323 if (r == match_result.no_match) continue;321 MatchResult r = m_options[i].match(name, approx); 322 323 if (r == MatchResult.no_match) continue; 324 324 325 325 // If we have a full match, and an approximate match, … … 332 332 // two full matches. 333 333 334 if (r == match_result.full_match) {334 if (r == MatchResult.full_match) { 335 335 return m_options[i]; 336 336 } … … 373 373 if (!opt.hidden) { 374 374 string ss; 375 ss = " " ~ opt.format _name() ~ ' ' ~ opt.format_parameter();375 ss = " " ~ opt.formatName() ~ ' ' ~ opt.formatParameter(); 376 376 width = max!(uint)(width, ss.length); 377 377 } … … 385 385 if (belong_to_group[i] || m_options[i].hidden==true) continue; 386 386 OptionDescription opt = m_options[i]; 387 result~=format _one(opt, width, m_line_length);387 result~=formatOne(opt, width, m_line_length); 388 388 result~='\n'; 389 389 } … … 424 424 425 425 426 string format _paragraph(string par, uint indent, uint line_length) {426 string formatParagraph(string par, uint indent, uint line_length) { 427 427 string result; 428 428 … … 522 522 //------------------------------------------------------------------------------ 523 523 524 string format _description(string desc, uint first_column_width, uint line_length) {524 string formatDescription(string 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 … … 563 563 string[] lines=splitlines(desc); 564 564 foreach(line; lines) { 565 result~=format _paragraph(line, first_column_width, line_length);565 result~=formatParagraph(line, first_column_width, line_length); 566 566 567 567 // prepair next line if any … … 578 578 //------------------------------------------------------------------------------ 579 579 580 string format _one(OptionDescription opt, uint first_column_width, uint line_length) {581 string result=" " ~ opt.format _name() ~ ' ' ~ opt.format_parameter();580 string formatOne(OptionDescription opt, uint first_column_width, uint line_length) { 581 string result=" " ~ opt.formatName() ~ ' ' ~ opt.formatParameter(); 582 582 583 583 if (opt.description()!="") { … … 585 585 result~=' '; 586 586 587 result~=format _description(opt.description(), first_column_width, line_length);587 result~=formatDescription(opt.description(), first_column_width, line_length); 588 588 } 589 589 return result; trunk/doost/program_options/positional_options.d
r5 r9 55 55 be present. Can return uint.max() to 56 56 indicate unlimited number. */ 57 uint max _total_count() {57 uint maxTotalCount() { 58 58 return m_trailing=="" ? m_names.length : uint.max; 59 59 } … … 63 63 Precondition: position < max_total_count() 64 64 */ 65 string name _for_position(uint position) {66 assert(position < max _total_count());65 string nameForPosition(uint position) { 66 assert(position < maxTotalCount()); 67 67 68 68 if (position < m_names.length) return m_names[position]; trunk/doost/program_options/value_semantic.d
r5 r9 36 36 /** The minimum number of tokens for this option that 37 37 should be present on the command line. */ 38 uint min _tokens();38 uint minTokens(); 39 39 40 40 /** The maximum number of tokens for this option that 41 41 should be present on the command line. */ 42 uint max _tokens();42 uint maxTokens(); 43 43 44 44 /** Returns true if values from different sources should be composed. … … 46 46 other sources are discarded. 47 47 */ 48 bool is _composing();48 bool isComposing(); 49 49 50 50 /** Parses a group of tokens that specify a value of option. … … 58 58 true if default value is assigned, and false if no default 59 59 value exists. */ 60 bool apply _default(inout Any value_store);60 bool applyDefault(inout Any value_store); 61 61 62 62 /** Called when final value of an option is determined. … … 79 79 } 80 80 81 override uint min _tokens() {81 override uint minTokens() { 82 82 if (m_zero_tokens) return 0; 83 83 else return 1; 84 84 } 85 85 86 override uint max _tokens() {86 override uint maxTokens() { 87 87 if (m_zero_tokens) return 0; 88 88 else return 1; 89 89 } 90 90 91 override bool is _composing() {91 override bool isComposing() { 92 92 return false; 93 93 } … … 106 106 107 107 /** Does nothing. */ 108 override bool apply _default(inout Any b) {108 override bool applyDefault(inout Any b) { 109 109 return false; 110 110 } … … 127 127 // Returns the type of the value described by this 128 128 // object. 129 TypeInfo value _type();130 } ;129 TypeInfo valueType(); 130 } 131 131 132 132 //------------------------------------------------------------------------------ … … 150 150 provide operator<< for ostream. 151 151 */ 152 TypedValue default _value(T v) {152 TypedValue defaultValue(T v) { 153 153 m_default_value.assign(v); 154 154 //NOTE: more elegant: toString() - format needs when v = e.g. string[] … … 163 163 by the user. 164 164 */ 165 TypedValue default _value(T v, string textual) {165 TypedValue defaultValue(T v, string textual) { 166 166 m_default_value.assign(v); 167 167 m_default_value_as_text = textual; … … 190 190 } 191 191 192 TypedValue zero _tokens() {192 TypedValue zeroTokens() { 193 193 m_zero_tokens = true; 194 194 return this; … … 205 205 } 206 206 207 bool is _composing() {207 bool isComposing() { 208 208 return m_composing; 209 209 } 210 210 211 override uint min _tokens() {211 override uint minTokens() { 212 212 if (m_zero_tokens) return 0; 213 213 else return 1; 214 214 } 215 215 216 override uint max _tokens() {216 override uint maxTokens() { 217 217 if (m_multitoken) { 218 218 return 32000; … … 234 234 Returns true if default value was stored. 235 235 */ 236 override bool apply _default(inout Any value_store) {236 override bool applyDefault(inout Any value_store) { 237 237 if (m_default_value.empty()) return false; 238 238 value_store.assign(m_default_value); … … 253 253 } 254 254 255 override TypeInfo value _type() {255 override TypeInfo valueType() { 256 256 return typeid(T); 257 257 } … … 291 291 TypedValue!(bool) bool_switch(bool* v=null) { 292 292 TypedValue!(bool) r = new TypedValue!(bool)(v); 293 r.default _value(0);294 r.zero _tokens();293 r.defaultValue(0); 294 r.zeroTokens(); 295 295 return r; 296 296 } … … 304 304 otherwise. */ 305 305 306 string get _single_string(string[] v, bool allow_empty = false) {306 string getSingleString(string[] 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]; … … 314 314 315 315 /* Throws MultipleOccurrences if 'value' is not empty. */ 316 void check _first_occurrence(Any value) {316 void checkFirstOccurrence(Any value) { 317 317 if (!value.empty()) 318 318 throw new MultipleOccurrences("multiple_occurrences"); … … 329 329 */ 330 330 void validate(T)(Any v, string[] xs) { 331 check _first_occurrence(v);332 string s=get _single_string(xs);331 checkFirstOccurrence(v); 332 string s=getSingleString(xs); 333 333 try { 334 334 //v = Any(lexical_cast<T>(s)); … … 377 377 */ 378 378 void validate(T : bool)(Any v, string[] xs) { 379 check _first_occurrence(v);380 string s=tolower(get _single_string(xs, true));379 checkFirstOccurrence(v); 380 string s=tolower(getSingleString(xs, true)); 381 381 382 382 if (s.length==0 || s == "on" || s == "yes" || s == "1" || s == "true") … … 391 391 392 392 void validate(T : string)(Any v, string[] xs) { 393 check _first_occurrence(v);394 string s=get _single_string(xs);393 checkFirstOccurrence(v); 394 string s=getSingleString(xs); 395 395 if ((s!="") && (s[0] == '\'' && s[$-1] == '\'' || s[0] == '"' && s[$-1] == '"')) 396 396 v.assign(s[1..$-2]); trunk/doost/program_options/variables_map.d
r5 r9 81 81 } 82 82 83 this(VariablesMap *next) {83 this(VariablesMap next) { 84 84 m_next=next; 85 85 } … … 109 109 110 110 111 if (v.empty() && m_next) return (*m_next)[name];111 if (v.empty() && m_next) return m_next[name]; 112 112 else if (v.defaulted() && m_next) { 113 VariableValue v2 = (*m_next)[name];113 VariableValue v2 = m_next[name]; 114 114 if (!v2.empty() && !v2.defaulted()) return v2; 115 115 else return v; … … 119 119 /** Sets next variable map, which will be used to find 120 120 variables not found in *this. */ 121 void next(VariablesMap *next) {121 void next(VariablesMap next) { 122 122 m_next = next; 123 123 } 124 124 125 // Moja funkcja - czy potrzebna? 126 VariableValue* opIn_r(string name) { 127 return (name in m_variables_map); 125 VariableValue opIn_r(string name) { 126 return opIndex(name); 128 127 } 129 128 … … 141 140 } 142 141 143 VariablesMap *m_next;142 VariablesMap m_next; 144 143 VariableValue[string] m_variables_map; 145 144 … … 216 215 d.semantic().parse(xm.m_variables_map[name].value(), options.options[i].value); 217 216 } catch (ValidationError e) { 218 e.set _option_name(name);217 e.setOptionName(name); 219 218 throw e; 220 219 } … … 226 225 // so that several assignment inside *this* 'store' call 227 226 // are allowed. 228 if (!d.semantic ().is_composing()) new_final[name]=true;227 if (!d.semantic.isComposing) new_final[name]=true; 229 228 } 230 229 … … 242 241 // internals. 243 242 // The 'key' is empty if options description contains '*'. 244 // In that 245 // case, default value makes no sense at all. 243 // In that case, default value makes no sense at all. 246 244 if (key.length==0) continue; 247 245 if (!(key in xm.m_variables_map)) { 248 246 249 247 Any def=new Any; 250 if (d.semantic ().apply_default(def)) {248 if (d.semantic.applyDefault(def)) { 251 249 // writefln("Istnieje defalut dla: ", key); 252 250 xm.m_variables_map[key] = new VariableValue(def, true); trunk/examples/program_options/custom_syntax.d
r8 r9 43 43 try { 44 44 auto desc = new OptionsDescription("Allowed options"); 45 desc.add _options()45 desc.addOptions() 46 46 ("help", "produce a help message") 47 47 ("foo", value!(string)(), "just an option") … … 49 49 50 50 auto vm = new VariablesMap; 51 store((new CommandLineParser(args)).options(desc).extra _parser(delegate OptionPair (string s) { return reg_foo(s); }).run(), vm);51 store((new CommandLineParser(args)).options(desc).extraParser(delegate OptionPair (string s) { return reg_foo(s); }).run(), vm); 52 52 53 53 if ("help" in vm) { trunk/examples/program_options/first.d
r8 r9 20 20 21 21 auto desc = new OptionsDescription("Allowed options"); 22 desc.add _options()22 desc.addOptions() 23 23 ("help", "produce help message") 24 24 ("compression", value!(int)(), "set compression level") … … 26 26 27 27 auto vm = new VariablesMap; 28 store(parse _command_line(args, desc), vm);28 store(parseCommandLine(args, desc), vm); 29 29 notify(vm); 30 30 trunk/examples/program_options/multiple_sources.d
r8 r9 26 26 // allowed only on command line 27 27 auto generic = new OptionsDescription("Generic options"); 28 generic.add _options()28 generic.addOptions() 29 29 //Extension - special case when you declare selfdir and selfname in one 30 30 //case … … 38 38 // config file 39 39 auto config = new OptionsDescription("Configuration"); 40 config.add _options()41 ("optimization", value!(int)(&opt).default _value(10), "optimization level")40 config.addOptions() 41 ("optimization", value!(int)(&opt).defaultValue(10), "optimization level") 42 42 ("include-path,I", value!(string[])().composing(), "include path") 43 43 ; … … 46 46 // in config file, but will not be shown to the user. 47 47 auto hidden=new OptionsDescription("Hidden options"); 48 hidden.add _options()48 hidden.addOptions() 49 49 ("input-file", value!(string[])(), "input file") 50 50 ; … … 69 69 // Parser
