Changeset 93
- Timestamp:
- 02/04/07 00:56:59 (2 years ago)
- Files:
-
- misc/optparse.d (modified) (6 diffs)
- misc/optparse.html (modified) (5 diffs)
- misc/opttest.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
misc/optparse.d
r82 r93 26 26 27 27 import std.stdio : writefln, writef; 28 import std.string : find, toupper ;28 import std.string : find, toupper, toString; 29 29 import std.c.stdlib : exit, EXIT_FAILURE, EXIT_SUCCESS; 30 30 import std.conv : toInt, ConvError; … … 180 180 char[] name, argname; 181 181 char[] const_value; 182 183 char[] default_string; 184 int default_value; 185 bool default_flag, has_default; 186 182 187 OptionCallbackArg callback; 183 188 OptionCallbackInt int_callback; … … 203 208 this.name = name; 204 209 this.argname = toupper(name); 210 this.default_string = ""; 211 this.default_value = 0; 212 this.default_flag = false; 205 213 206 214 // Perform sanity checks. … … 265 273 return result; 266 274 } 275 //enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } 276 void issue_default(Options results) { 277 // Only set the default if the option doesn't already have a value. 278 char[][]* val = this.name in results.opts; 279 switch (this.action) { 280 case Action.Store, Action.Append: 281 if (val !is null) return; 282 if (this.type == ArgType.String) { 283 results.opts[name] = [default_string]; 284 } else { 285 results.opts[name] = [.toString(default_value)]; 286 } 287 break; 288 case Action.StoreConst, Action.AppendConst: 289 if (val !is null) return; 290 results.opts[name] = [default_string]; 291 break; 292 case Action.SetTrue, Action.SetFalse: 293 if (val !is null) return; 294 if (default_flag) { 295 results.opts[name] = ["1"]; 296 } else { 297 results.opts[name] = ["0"]; 298 } 299 break; 300 default: 301 return; 302 } 303 } 267 304 // Does whatever this option is supposed to do. 268 305 void perform_action(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { … … 338 375 Option argName(char[] argname) { 339 376 this.argname = argname; 377 return this; 378 } 379 Option def(char[] val) { 380 if ( 381 (this.type != ArgType.String || (this.action != Action.Store && this.action != Action.Append)) && 382 this.action != Action.StoreConst && this.action != Action.AppendConst 383 ) 384 throw new OptionError("Cannot specify string default for non-string option '"~this.name~"'"); 385 this.has_default = true; 386 this.default_string = val; 387 return this; 388 } 389 Option def(int val) { 390 if (this.type != ArgType.Integer || (this.action != Action.Store && this.action != Action.Append)) 391 throw new OptionError("Cannot specify integer default for non-integer option '"~this.name~"'"); 392 this.has_default = true; 393 this.default_value = val; 394 return this; 395 } 396 Option def(bool val) { 397 if (this.action != Action.SetTrue && this.action != Action.SetFalse) 398 throw new OptionError("Cannot specify boolean default for non-flag option '"~this.name~"'"); 399 this.has_default = true; 400 this.default_flag = val; 340 401 return this; 341 402 } … … 552 613 } 553 614 } 615 } 616 foreach (o; this.options) { 617 o.issue_default(options); 554 618 } 555 619 return options; misc/optparse.html
r82 r93 135 135 <blockquote><tt>int num = options.value("number");</tt></blockquote> 136 136 137 <p>If the option was not provided on the command line, <tt>opIndex</tt> will return the empty string, and <tt>value</tt> will return 0.</p>137 <p>If the option was not provided on the command line, and no default was supplied with <tt>def</tt>, <tt>opIndex</tt> will return the empty string, and <tt>value</tt> will return 0.</p> 138 138 139 139 <h4>Example</h4> … … 176 176 <blockquote><tt>int[] values = options.value_list("value");</tt></blockquote> 177 177 178 <p>If the option is not provided on the command line, these will both return an empty array.</p>178 <p>If the option is not provided on the command line, and no default was provided with <tt>def</tt>, these will both return an empty array.</p> 179 179 180 180 <h4>Example</h4> … … 210 210 211 211 <h4>Results</h4> 212 <p>Accessing the results of a <tt>StoreConst</tt> option is exactly like accessing those of a <tt>Store</tt> action with a <tt>String</tt> type. (Using opIndex.) If the option was specified, the constant value will be returned. If it wasn't, it will return an empty string.</p>212 <p>Accessing the results of a <tt>StoreConst</tt> option is exactly like accessing those of a <tt>Store</tt> action with a <tt>String</tt> type. (Using opIndex.) If the option was specified, the constant value will be returned. If it wasn't, and no default was provided with <tt>def</tt>, it will return an empty string.</p> 213 213 214 214 <p>At this time, only strings are supported as constant values.</p> … … 274 274 <blockquote><tt>bool flag = options.flag("flag");</tt></blockquote> 275 275 276 <p>If the flag was not specified, it defaults to false.</p>276 <p>If the flag was not specified, and no default was provided with <tt>def</tt>, it defaults to false.</p> 277 277 278 278 <h4>Example</h4> … … 574 574 <dt><tt>Option argName(char[] argname);</tt></dt> <dd>When an option accepts an argument, the <tt>Help</tt> action will indicate this by putting something informative after the option. By default, it uses the all-caps form of the option's name. You can supply a different string to this method, which will be converted to all-caps.</dd> 575 575 576 <dt><tt>Option def(char[] val);</tt></dt> 577 <dt><tt>Option def(int val);</tt></dt> 578 <dt><tt>Option def(bool val);</tt></dt> 579 <dd>Supplies the default value for an option. This only applies to the following actions: <tt>Store</tt>, <tt>Append</tt>, <tt>StoreConst</tt>, <tt>AppendConst</tt>, <tt>SetTrue</tt>, and <tt>SetFalse</tt>. In the case of the two "append" actions, supplying a default will result in a single-element array with the default in it, in the event that the option is never specified on the command-line. If multiple options have the same name, only the first default supplied is used. An exception will be raised if (for example) an integer default value is provided to an option which expects a string argument.</dd> 580 576 581 <dt><tt>bool hasArg();</tt></dt> <dd>Returns whether this option accepts an argument.</dd> 577 582 misc/opttest.d
r79 r93 10 10 writefln("number list: ", options.value_list("list")); 11 11 writefln("switch: ", options.flag("switch")); 12 writefln("turn-off: ", options.flag("turn")); 13 writefln("color: ", options["color"]); 12 14 writefln("args: ", options.args); 13 15 } … … 33 35 parser.add_option(["--enable"], "switch", Action.SetTrue).help("Enables the switch."); 34 36 parser.add_option(["--disable"], "switch", Action.SetFalse).help("Disables the switch."); 37 parser.add_option(["--turn-off"], "turn", Action.SetFalse).help("Tests default values for flags.").def(true); 38 parser.add_option(["--set-blue"], "color", Action.StoreConst, "blue").help("Tests default values for consts.").def("green"); 35 39 parser.add_option(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER"); 36 40 parser.add_option(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER");
