Changeset 99
- Timestamp:
- 02/08/07 01:01:54 (2 years ago)
- Files:
-
- misc/optparse.d (modified) (28 diffs)
- misc/optparse.html (modified) (33 diffs)
- misc/opttest.d (modified) (3 diffs)
- misc/opttest_tango.d (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
misc/optparse.d
r93 r99 22 22 /** 23 23 * Command-line option parsing, in the style of Python's optparse. 24 * 25 * Refer to the complete docs for more information. 24 26 */ 25 27 module optparse; 26 28 27 import std.stdio : writefln, writef; 28 import std.string : find, toupper, toString; 29 import std.c.stdlib : exit, EXIT_FAILURE, EXIT_SUCCESS; 30 import std.conv : toInt, ConvError; 31 import std.path : getBaseName; 32 import std.utf : toUTF8, toUTF32; 29 version (Tango) { 30 import tango.io.Stdout : Stdout; 31 import tango.text.Util : find = locate, locatePrior; 32 import tango.text.Ascii : toupper = toUpper; 33 import tango.stdc.stdlib : exit, EXIT_FAILURE, EXIT_SUCCESS; 34 import tango.text.convert.Integer : parse, toInt, toString = toUtf8; 35 import tango.text.convert.Utf : toUTF8 = toUtf8, toUTF32 = toUtf32; 36 int getNotFound(char[] s) { 37 return s.length; 38 } 39 } else { 40 import std.stdio : writefln, writef; 41 import std.string : find, toupper, toString; 42 import std.c.stdlib : exit, EXIT_FAILURE, EXIT_SUCCESS; 43 import std.conv : toInt, ConvError; 44 import std.path : getBaseName; 45 import std.utf : toUTF8, toUTF32; 46 int getNotFound(char[] s) { 47 return -1; 48 } 49 } 33 50 34 51 /* … … 74 91 } 75 92 76 // Thrown if client code tries to set up an improper option.93 /// Thrown if client code tries to set up an improper option. 77 94 class OptionError : Exception { 78 95 this(char[] msg) { super(msg); } … … 83 100 } 84 101 102 /++ 103 This class represents the results after parsing the command-line. 104 +/ 85 105 class Options { 86 106 char[][][char[]] opts; 87 107 int[char[]] counted_opts; 108 /// By default, leftover arguments are placed in this array. 88 109 char[][] args; 89 110 111 /// Retrieves the results of the Store and StoreConst actions. 90 112 char[] opIndex(char[] opt) { 91 113 char[][]* o = opt in opts; … … 96 118 } 97 119 } 120 /// Retrieves the results of the Store action, when the type is Integer. 98 121 int value(char[] opt) { 99 122 char[][]* o = opt in opts; … … 104 127 } 105 128 } 129 /// Retrieves the results of the Append and AppendConst actions. 106 130 char[][] list(char[] opt) { 107 131 char[][]* o = opt in opts; … … 112 136 } 113 137 } 114 int[] value_list(char[] opt) { 138 /// Retrieves the results of the Append action, when the type is Integer. 139 int[] valueList(char[] opt) { 115 140 char[][]* o = opt in opts; 116 141 int[] l; … … 123 148 return l; 124 149 } 150 /// Retrieves the results of the Count action. 125 151 int count(char[] opt) { 126 152 int* c = opt in counted_opts; … … 131 157 } 132 158 } 159 /// Retrieves the results of the SetTrue and SetFalse actions. 133 160 bool flag(char[] opt) { 134 161 char[][]* o = opt in opts; … … 142 169 143 170 // Options, args, this opt's index in args, name[, arg] 171 /// 144 172 alias void delegate(Options, inout char[][], inout int, char[], char[]) OptionCallbackFancyArg; 173 /// 145 174 alias void delegate(Options, inout char[][], inout int, char[], int) OptionCallbackFancyInt; 175 /// 146 176 alias void delegate(Options, inout char[][], inout int, char[]) OptionCallbackFancy; 147 177 178 /// 148 179 alias void delegate(char[]) OptionCallbackArg; 180 /// 149 181 alias void delegate(int) OptionCallbackInt; 182 /// 150 183 alias void delegate() OptionCallback; 151 184 … … 160 193 * CallbackVoid: dg 161 194 */ 162 enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } 163 enum ArgType { None, String, Integer } 195 /// 196 enum Action { /+++/Store, /+++/StoreConst, /+++/Append, /+++/AppendConst, /+++/Count, /+++/SetTrue, /+++/SetFalse, /+++/Callback, /+++/CallbackFancy, /+++/Help /+++/} 197 /// 198 enum ArgType { /+++/None, /+++/String, /+++/Integer /+++/} 164 199 165 200 ArgType defaultType(Action action) { … … 174 209 } 175 210 211 /++ 212 This class represents a single command-line option. 213 +/ 176 214 class Option { 177 215 char[][] shortopts, longopts; … … 207 245 this.type = type; 208 246 this.name = name; 209 this.argname = toupper(name );247 this.argname = toupper(name.dup); 210 248 this.default_string = ""; 211 249 this.default_value = 0; … … 268 306 result ~= ", "; 269 307 } else if (this.hasArg()) { 270 result ~= "=" ~ toupper(this.argname );308 result ~= "=" ~ toupper(this.argname.dup); 271 309 } 272 310 } … … 303 341 } 304 342 // Does whatever this option is supposed to do. 305 void perform _action(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) {343 void performAction(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { 306 344 int i; 307 345 if (this.type == ArgType.Integer) { … … 361 399 break; 362 400 case Action.Help: 363 parser.help _text();401 parser.helpText(); 364 402 exit(EXIT_SUCCESS); 365 403 break; 366 404 } 367 405 } 406 /// Returns whether this option accepts an argument. 368 407 bool hasArg() { 369 408 return this.type != ArgType.None; 370 409 } 410 /// Sets the help text for this option. 371 411 Option help(char[] help) { 372 412 this.helptext = help; 373 413 return this; 374 414 } 415 /// Sets the name of this option's argument, if it has one. 375 416 Option argName(char[] argname) { 376 417 this.argname = argname; … … 428 469 } 429 470 471 /++ 472 This class is used to define a set of options, and parse the command-line 473 arguments. 474 +/ 430 475 class OptionParser { 431 476 OptionCallbackArg leftover_cb; 477 /// An array of all of the options known by this parser. 432 478 Option[] options; 433 char[] name, desc, argdesc; 479 char[] name, desc; 480 /// The description of the programs arguments, as used in the Help action. 481 char[] argdesc; 434 482 private void delegate(char[]) error_callback; 435 483 … … 440 488 } 441 489 442 void set_error_callback(void delegate(char[]) dg) { 490 /// Sets a callback, to override the default error behavior. 491 void setErrorCallback(void delegate(char[]) dg) { 443 492 error_callback = dg; 444 493 } 445 void unknown _opt_error(char[] opt) {494 void unknownOptError(char[] opt) { 446 495 error("Unknown argument '"~opt~"'"); 447 496 } 448 void expected _arg_error(char[] opt) {497 void expectedArgError(char[] opt) { 449 498 error("'"~opt~"' option expects an argument."); 450 499 } 500 /// Displays an error message and terminates the program. 451 501 void error(char[] err) { 452 502 if (error_callback !is null) { 453 503 error_callback(err); 454 504 } else { 455 this.help_text(); 456 writefln(err); 505 this.helpText(); 506 version (Tango) { 507 Stdout.formatln(err); 508 } else { 509 writefln(err); 510 } 457 511 } 458 512 exit(EXIT_FAILURE); 459 513 } 460 int toOptInt(char[] s) { 461 int i; 462 try { 463 i = toInt(s); 464 } catch (ConvError e) { 465 error("Could not convert '"~s~"' to an integer."); 466 } 467 return i; 468 } 469 470 void help_text() { 514 version (Tango) { 515 int toOptInt(char[] s) { 516 int i; 517 uint ate; 518 i = .parse(s, 10u, &ate); 519 if (ate != s.length) 520 error("Could not convert '"~s~"' to an integer."); 521 return i; 522 } 523 } else { 524 int toOptInt(char[] s) { 525 int i; 526 try { 527 i = toInt(s); 528 } catch (ConvError e) { 529 error("Could not convert '"~s~"' to an integer."); 530 } 531 return i; 532 } 533 } 534 535 /// Displays useful "help" information about the program's options. 536 void helpText() { 471 537 int optWidth; 472 538 char[][] optStrs; … … 478 544 } 479 545 } 480 writefln("Usage: %s %s", this.name, this.argdesc); 481 if (this.desc !is null && this.desc != "") writefln(this.desc); 482 writefln("\nOptions:"); 483 foreach(i, opt; options) { 484 // The commented-out code is a great idea I'll do properly later. 485 //if (opt.helptext.length + optWidth + 3 > 80) { 486 // writefln(" %s\n\t%s", optStrs[i], opt.helptext); 487 //} else { 488 writefln(" %-*s %s", optWidth, optStrs[i], opt.helptext); 489 //} 546 version (Tango) { 547 typedef char spacechar = ' '; 548 spacechar[] padding; 549 Stdout.formatln("Usage: {0} {1}", this.name, this.argdesc); 550 if (this.desc !is null && this.desc != "") Stdout.println(this.desc); 551 Stdout.println("\nOptions:"); 552 foreach(i, opt; options) { 553 padding.length = optWidth - optStrs[i].length; 554 Stdout.formatln(" {0}{1} {2}", optStrs[i], cast(char[])padding, opt.helptext); 555 } 556 } else { 557 writefln("Usage: %s %s", this.name, this.argdesc); 558 if (this.desc !is null && this.desc != "") writefln(this.desc); 559 writefln("\nOptions:"); 560 foreach(i, opt; options) { 561 // The commented-out code is a great idea I'll do properly later. 562 //if (opt.helptext.length + optWidth + 3 > 80) { 563 // writefln(" %s\n\t%s", optStrs[i], opt.helptext); 564 //} else { 565 writefln(" %-*s %s", optWidth, optStrs[i], opt.helptext); 566 //} 567 } 490 568 } 491 569 } … … 501 579 return null; 502 580 } 503 char[] get_program_name(char[] path) { 504 version(Windows) { 505 // (Unicode note: ".exe" only contains 4 code units, so this slice 506 // should Just Work.) (Although it remains to be seen how robust 507 // this code actually is.) 508 assert(path[$-4 .. $] == ".exe"); 509 path = path[0 .. $-4]; 510 } 511 return getBaseName(path); 512 } 513 Options parse_args(char[][] args) { 514 this.name = get_program_name(args[0]); 581 version (Tango) { 582 char[] getProgramName(char[] path) { 583 version(Windows) { 584 char delimiter = '\\'; 585 } else { 586 char delimiter = '/'; 587 } 588 uint idx = locatePrior(path, delimiter); 589 if (idx == path.length) return path; 590 return path[idx+1 .. $]; 591 } 592 } else { 593 char[] getProgramName(char[] path) { 594 version(Windows) { 595 // (Unicode note: ".exe" only contains 4 code units, so this slice 596 // should Just Work.) (Although it remains to be seen how robust 597 // this code actually is.) 598 assert(path[$-4 .. $] == ".exe"); 599 path = path[0 .. $-4]; 600 } 601 return getBaseName(path); 602 } 603 } 604 /// Parses the passed command-line arguments and returns the results. 605 Options parse(char[][] args) { 606 this.name = getProgramName(args[0]); 515 607 args = args[1 .. $]; 516 608 Options options = new Options; … … 554 646 } else if (opt.startswith("--")) { 555 647 idx = find(opt, '='); 556 if (idx != -1) {648 if (idx != getNotFound(opt)) { 557 649 newopt = opt[0 .. idx]; 558 650 // Stitch out the old arg, stitch in the newopt, arg pair. … … 565 657 match = matches(newopt); 566 658 if (match is null) { 567 unknown _opt_error(newopt);659 unknownOptError(newopt); 568 660 } 569 661 if (match.hasArg) { 570 if (i == args.length-1) expected _arg_error(match.name);662 if (i == args.length-1) expectedArgError(match.name); 571 663 arg = args[i+1]; 572 664 ++i; … … 574 666 arg = null; 575 667 } 576 match.perform _action(this, options, args, i, arg);668 match.performAction(this, options, args, i, arg); 577 669 } else if (opt.startswith("-")) { 578 670 if (opt.length >= 2) { … … 582 674 match = matches(newopt); 583 675 if (match is null) { 584 unknown _opt_error(newopt);676 unknownOptError(newopt); 585 677 } 586 678 if (match.hasArg) { … … 588 680 // next element of args for the arg. 589 681 if (j == opt32.length-1) { 590 if (i == args.length-1) expected _arg_error(match.name);682 if (i == args.length-1) expectedArgError(match.name); 591 683 arg = args[i+1]; 592 684 ++i; … … 595 687 } else { 596 688 arg = toUTF8(opt32[j+1 .. $]); 597 match.perform _action(this, options, args, i, arg);689 match.performAction(this, options, args, i, arg); 598 690 break; 599 691 } … … 601 693 arg = null; 602 694 } 603 match.perform _action(this, options, args, i, arg);695 match.performAction(this, options, args, i, arg); 604 696 } 605 697 } else { 606 unknown _opt_error(opt);698 unknownOptError(opt); 607 699 } 608 700 } else { … … 620 712 } 621 713 622 void leftover_callback(OptionCallbackArg dg) { 714 /++ 715 Overrides the default behavior of leftover arguments, calling this callback 716 with them instead of adding them an array. 717 +/ 718 void leftoverCallback(OptionCallbackArg dg) { 623 719 this.leftover_cb = dg; 624 720 } 625 Option add_option(Option option) { 721 /// 722 Option addOption(Option option) { 626 723 this.options ~= option; 627 724 return option; 628 725 } 629 726 // options action name type const_value dga dgv dgi fdga fdgi fdg 630 Option add_option(char[][] options ...) { 631 return add_option(options, Action.Store, null, defaultType(Action.Store), null, null, null, null, null, null, null); 632 } 633 Option add_option(char[][] options, char[] name) { 634 return add_option(options, Action.Store, name, defaultType(Action.Store), null, null, null, null, null, null, null); 635 } 636 Option add_option(char[][] options, Action action) { 637 return add_option(options, action, null, defaultType(action), null, null, null, null, null, null, null); 638 } 639 Option add_option(char[][] options, ArgType type) { 640 return add_option(options, Action.Store, null, type, null, null, null, null, null, null, null); 641 } 642 Option add_option(char[][] options, Action action, ArgType type) { 643 return add_option(options, action, null, type, null, null, null, null, null, null, null); 644 } 645 Option add_option(char[][] options, char[] name, Action action) { 646 return add_option(options, action, name, defaultType(action), null, null, null, null, null, null, null); 647 } 648 Option add_option(char[][] options, char[] name, Action action, ArgType type) { 649 return add_option(options, action, name, type, null, null, null, null, null, null, null); 650 } 651 Option add_option(char[][] options, Action action, char[] const_value) { 652 return add_option(options, action, null, defaultType(action), const_value, null, null, null, null, null, null); 653 } 654 Option add_option(char[][] options, char[] name, char[] const_value) { 655 return add_option(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null, null, null); 656 } 657 Option add_option(char[][] options, char[] name, Action action, char[] const_value) { 658 return add_option(options, action, name, defaultType(action), const_value, null, null, null, null, null, null); 659 } 660 Option add_option(char[][] options, OptionCallbackArg dg) { 661 return add_option(options, Action.Callback, null, ArgType.String, null, dg, null, null, null, null, null); 662 } 663 Option add_option(char[][] options, OptionCallback dg) { 664 return add_option(options, Action.Callback, null, ArgType.None, null, null, dg, null, null, null, null); 665 } 666 Option add_option(char[][] options, OptionCallbackInt dg) { 667 return add_option(options, Action.Callback, null, ArgType.Integer, null, null, null, dg, null, null, null); 668 } 669 Option add_option(char[][] options, OptionCallbackFancyArg dg) { 670 return add_option(options, Action.CallbackFancy, null, ArgType.String, null, null, null, null, dg, null, null); 671 } 672 Option add_option(char[][] options, OptionCallbackFancy dg) { 673 return add_option(options, Action.CallbackFancy, null, ArgType.None, null, null, null, null, null, null, dg); 674 } 675 Option add_option(char[][] options, OptionCallbackFancyInt dg) { 676 return add_option(options, Action.CallbackFancy, null, ArgType.Integer, null, null, null, null, null, dg, null); 677 } 678 Option add_option(char[][] options, char[] name, OptionCallbackFancyArg dg) { 679 return add_option(options, Action.CallbackFancy, name, ArgType.String, null, null, null, null, dg, null, null); 680 } 681 Option add_option(char[][] options, char[] name, OptionCallbackFancy dg) { 682 return add_option(options, Action.CallbackFancy, name, ArgType.None, null, null, null, null, null, null, dg); 683 } 684 Option add_option(char[][] options, char[] name, OptionCallbackFancyInt dg) { 685 return add_option(options, Action.CallbackFancy, name, ArgType.Integer, null, null, null, null, null, dg, null); 727 /// 728 Option addOption(char[][] options ...) { 729 return addOption(options, Action.Store, null, defaultType(Action.Store), null, null, null, null, null, null, null); 730 } 731 /// 732 Option addOption(char[][] options, char[] name) { 733 return addOption(options, Action.Store, name, defaultType(Action.Store), null, null, null, null, null, null, null); 734 } 735 /// 736 Option addOption(char[][] options, Action action) { 737 return addOption(options, action, null, defaultType(action), null, null, null, null, null, null, null); 738 } 739 /// 740 Option addOption(char[][] options, ArgType type) { 741 return addOption(options, Action.Store, null, type, null, null, null, null, null, null, null); 742 } 743 /// 744 Option addOption(char[][] options, Action action, ArgType type) { 745 return addOption(options, action, null, type, null, null, null, null, null, null, null); 746 } 747 /// 748 Option addOption(char[][] options, char[] name, Action action) { 749 return addOption(options, action, name, defaultType(action), null, null, null, null, null, null, null); 750 } 751 /// 752 Option addOption(char[][] options, char[] name, Action action, ArgType type) { 753 return addOption(options, action, name, type, null, null, null, null, null, null, null); 754 } 755 /// 756 Option addOption(char[][] options, Action action, char[] const_value) { 757 return addOption(options, action, null, defaultType(action), const_value, null, null, null, null, null, null); 758 } 759 /// 760 Option addOption(char[][] options, char[] name, char[] const_value) { 761 return addOption(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null, null, null); 762 } 763 /// 764 Option addOption(char[][] options, char[] name, Action action, char[] const_value) { 765 return addOption(options, action, name, defaultType(action), const_value, null, null, null, null, null, null); 766 } 767 /// 768 Option addOption(char[][] options, OptionCallbackArg dg) { 769 return addOption(options, Action.Callback, null, ArgType.String, null, dg, null, null, null, null, null); 770 } 771 /// 772 Option addOption(char[][] options, OptionCallback dg) { 773 return addOption(options, Action.Callback, null, ArgType.None, null, null, dg, null, null, null, null); 774 } 775 /// 776 Option addOption(char[][] options, OptionCallbackInt dg) { 777 return addOption(options, Action.Callback, null, ArgType.Integer, null, null, null, dg, null, null, null); 778 } 779 /// 780 Option addOption(char[][] options, OptionCallbackFancyArg dg) { 781 return addOption(options, Action.CallbackFancy, null, ArgType.String, null, null, null, null, dg, null, null); 782 } 783 /// 784 Option addOption(char[][] options, OptionCallbackFancy dg) { 785 return addOption(options, Action.CallbackFancy, null, ArgType.None, null, null, null, null, null, null, dg); 786 } 787 /// 788 Option addOption(char[][] options, OptionCallbackFancyInt dg) { 789 return addOption(options, Action.CallbackFancy, null, ArgType.Integer, null, null, null, null, null, dg, null); 790 } 791 /// 792 Option addOption(char[][] options, char[] name, OptionCallbackFancyArg dg) { 793 return addOption(options, Action.CallbackFancy, name, ArgType.String, null, null, null, null, dg, null, null); 794 } 795 /// 796 Option addOption(char[][] options, char[] name, OptionCallbackFancy dg) { 797 return addOption(options, Action.CallbackFancy, name, ArgType.None, null, null, null, null, null, null, dg); 798 } 799 /// 800 Option addOption(char[][] options, char[] name, OptionCallbackFancyInt dg) { 801 return addOption(options, Action.CallbackFancy, name, ArgType.Integer, null, null, null, null, null, dg, null); 686 802 } 687 803 // Although users certainly /can/ call this, all those overloads are there 688 804 // for a reason. 689 Option add _option(805 Option addOption( 690 806 char[][] options, 691 807 Action action, char[] name, ArgType type, char[] const_value, … … 731 847 else 732 848 throw new OptionError( 733 "No options provided to add _option!"849 "No options provided to addOption!" 734 850 ); 735 851 } misc/optparse.html
r93 r99 44 44 <blockquote><tt>OptionParser(char[] desc="")</tt></blockquote> 45 45 46 <p>The <i>desc</i> parameter should be a short description of your program. It is used by the built-in help features, described later. <tt>OptionParser</tt> has a number of methods, the most important of which is <tt>add _option</tt>. The <tt>add_option</tt> method has a large number of overloads, representing the large number of kinds of options that <tt>optparse</tt> supports. Before going over these overloads, it is necessary to go over some of <tt>optparse</tt>'s concepts.</p>46 <p>The <i>desc</i> parameter should be a short description of your program. It is used by the built-in help features, described later. <tt>OptionParser</tt> has a number of methods, the most important of which is <tt>addOption</tt>. The <tt>addOption</tt> method has a large number of overloads, representing the large number of kinds of options that <tt>optparse</tt> supports. Before going over these overloads, it is necessary to go over some of <tt>optparse</tt>'s concepts.</p> 47 47 48 48 <p>First, this documentation makes the distinction between an <i>option</i> and an <i>argument</i>. An <i>option</i> is a literal string supplied by client code, such as "<tt>-f</tt>" or "<tt>--file</tt>". An <i>argument</i> is supplied by the end-user on the command-line. Some options accept arguments. If the user supplies "<tt>myapp --file=somefile.txt</tt>" to your program, then "<tt>--file</tt>" is an option, and "<tt>somefile.txt</tt>" is an argument to it. Arguments not associated with any particular option are called <i>leftover arguments</i>.</p> … … 69 69 <p>Options also have a <i>name</i>, which is used in a number of contexts. The name defaults to the first long option specified, without the dashes. If no long options are specified, it uses the first short option. An option's name is mainly used when retrieving its results, after parsing the arguments. Occasionally, it is useful to give multiple options the same name. Examples of this are given later.</p> 70 70 71 <p>For completeness' sake, here are all of the overloads of <tt>add _option</tt>:</p>72 73 <ul> 74 <li><tt>Option add _option(char[][] options ...)</tt></li>75 <li><tt>Option add _option(char[][] options, char[] name)</tt></li>76 <li><tt>Option add _option(char[][] options, Action action)</tt></li>77 <li><tt>Option add _option(char[][] options, ArgType type)</tt></li>78 <li><tt>Option add _option(char[][] options, Action action, ArgType type)</tt></li>79 <li><tt>Option add _option(char[][] options, char[] name, Action action)</tt></li>80 <li><tt>Option add _option(char[][] options, char[] name, Action action, ArgType type)</tt></li>81 <li><tt>Option add _option(char[][] options, Action action, char[] const_value)</tt></li>82 <li><tt>Option add _option(char[][] options, char[] name, char[] const_value)</tt></li>83 <li><tt>Option add _option(char[][] options, char[] name, Action action, char[] const_value)</tt></li>84 <li><tt>Option add _option(char[][] options, void delegate() dg)</tt></li>85 <li><tt>Option add _option(char[][] options, void delegate(char[]) dg)</tt></li>86 <li><tt>Option add _option(char[][] options, void delegate(int) dg)</tt></li>87 <li><tt>Option add _option(char[][] options, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li>88 <li><tt>Option add _option(char[][] options, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li>89 <li><tt>Option add _option(char[][] options, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li>90 <li><tt>Option add _option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li>91 <li><tt>Option add _option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li>92 <li><tt>Option add _option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li>93 </ul> 94 95 <p>As I describe each action, I will also list the relevant <tt>add _option</tt> overloads to that action. Not all of these apply to all of the actions.</p>71 <p>For completeness' sake, here are all of the overloads of <tt>addOption</tt>:</p> 72 73 <ul> 74 <li><tt>Option addOption(char[][] options ...)</tt></li> 75 <li><tt>Option addOption(char[][] options, char[] name)</tt></li> 76 <li><tt>Option addOption(char[][] options, Action action)</tt></li> 77 <li><tt>Option addOption(char[][] options, ArgType type)</tt></li> 78 <li><tt>Option addOption(char[][] options, Action action, ArgType type)</tt></li> 79 <li><tt>Option addOption(char[][] options, char[] name, Action action)</tt></li> 80 <li><tt>Option addOption(char[][] options, char[] name, Action action, ArgType type)</tt></li> 81 <li><tt>Option addOption(char[][] options, Action action, char[] const_value)</tt></li> 82 <li><tt>Option addOption(char[][] options, char[] name, char[] const_value)</tt></li> 83 <li><tt>Option addOption(char[][] options, char[] name, Action action, char[] const_value)</tt></li> 84 <li><tt>Option addOption(char[][] options, void delegate() dg)</tt></li> 85 <li><tt>Option addOption(char[][] options, void delegate(char[]) dg)</tt></li> 86 <li><tt>Option addOption(char[][] options, void delegate(int) dg)</tt></li> 87 <li><tt>Option addOption(char[][] options, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> 88 <li><tt>Option addOption(char[][] options, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> 89 <li><tt>Option addOption(char[][] options, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> 90 <li><tt>Option addOption(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> 91 <li><tt>Option addOption(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> 92 <li><tt>Option addOption(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> 93 </ul> 94 95 <p>As I describe each action, I will also list the relevant <tt>addOption</tt> overloads to that action. Not all of these apply to all of the actions.</p> 96 96 97 97 <p>A very simple use of optparse looks like this:</p> … … 99 99 <blockquote><pre>void main(char[][] args) { 100 100 auto parser = new OptionParser(); 101 parser.add _option("-o", "--option");102 auto options = parser.parse _args(args);101 parser.addOption("-o", "--option"); 102 auto options = parser.parse(args); 103 103 }</pre></blockquote> 104 104 … … 115 115 <h4>Methods</h4> 116 116 <ul> 117 <li><tt>Option add _option(char[][] options ...)</tt></li>118 <li><tt>Option add _option(char[][] options, char[] name)</tt></li>119 <li><tt>Option add _option(char[][] options, Action action)</tt></li>120 <li><tt>Option add _option(char[][] options, ArgType type)</tt></li>121 <li><tt>Option add _option(char[][] options, Action action, ArgType type)</tt></li>122 <li><tt>Option add _option(char[][] options, char[] name, Action action)</tt></li>123 <li><tt>Option add _option(char[][] options, char[] name, Action action, ArgType type)</tt></li>117 <li><tt>Option addOption(char[][] options ...)</tt></li> 118 <li><tt>Option addOption(char[][] options, char[] name)</tt></li> 119 <li><tt>Option addOption(char[][] options, Action action)</tt></li> 120 <li><tt>Option addOption(char[][] options, ArgType type)</tt></li> 121 <li><tt>Option addOption(char[][] options, Action action, ArgType type)</tt></li> 122 <li><tt>Option addOption(char[][] options, char[] name, Action action)</tt></li> 123 <li><tt>Option addOption(char[][] options, char[] name, Action action, ArgType type)</tt></li> 124 124 </ul> 125 125 … … 140 140 <blockquote><pre>void main(char[][] args) { 141 141 auto parser = new OptionParser(); 142 parser.add _option("-f", "--file");143 parser.add _option(["-n", "--number"], ArgType.Integer);144 auto options = parser.parse _args(args);142 parser.addOption("-f", "--file"); 143 parser.addOption(["-n", "--number"], ArgType.Integer); 144 auto options = parser.parse(args); 145 145 writefln("file: %s", options["file"]); 146 146 writefln("number: %s", options.value("number")); … … 161 161 <h4>Methods</h4> 162 162 <ul> 163 <li><tt>Option add _option(char[][] options, Action action)</tt></li>164 <li><tt>Option add _option(char[][] options, Action action, ArgType type)</tt></li>165 <li><tt>Option add _option(char[][] options, char[] name, Action action)</tt></li>166 <li><tt>Option add _option(char[][] options, char[] name, Action action, ArgType type)</tt></li>163 <li><tt>Option addOption(char[][] options, Action action)</tt></li> 164 <li><tt>Option addOption(char[][] options, Action action, ArgType type)</tt></li> 165 <li><tt>Option addOption(char[][] options, char[] name, Action action)</tt></li> 166 <li><tt>Option addOption(char[][] options, char[] name, Action action, ArgType type)</tt></li> 167 167 </ul> 168 168 … … 172 172 <blockquote><tt>char[][] imports = options.list("import");</tt></blockquote> 173 173 174 <p>If the type is <tt>Integer</tt>, use the <tt>value _list</tt> method:</p>175 176 <blockquote><tt>int[] values = options.value _list("value");</tt></blockquote>174 <p>If the type is <tt>Integer</tt>, use the <tt>valueList</tt> method:</p> 175 176 <blockquote><tt>int[] values = options.valueList("value");</tt></blockquote> 177 177 178 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> … … 181 181 <blockquote><pre>void main(char[][] args) { 182 182 auto parser = new OptionParser(); 183 parser.add _option(["-I", "--import"], Action.Append);184 parser.add _option(["-V", "--value"], Action.Append, ArgType.Integer);185 auto options = parser.parse _args(args);183 parser.addOption(["-I", "--import"], Action.Append); 184 parser.addOption(["-V", "--value"], Action.Append, ArgType.Integer); 185 auto options = parser.parse(args); 186 186 writefln("imports: %s", options.list("import")); 187 writefln("values: %s", options.value _list("value"));187 writefln("values: %s", options.valueList("value")); 188 188 }</pre></blockquote> 189 189 … … 204 204 <h4>Methods</h4> 205 205 <ul> 206 <li><tt>Option add _option(char[][] options, Action action, char[] const_value)</tt></li>207 <li><tt>Option add _option(char[][] options, char[] name, char[] const_value)</tt></li>208 <li><tt>Option add _option(char[][] options, char[] name, Action action, char[] const_value)</tt></li>206 <li><tt>Option addOption(char[][] options, Action action, char[] const_value)</tt></li> 207 <li><tt>Option addOption(char[][] options, char[] name, char[] const_value)</tt></li> 208 <li><tt>Option addOption(char[][] options, char[] name, Action action, char[] const_value)</tt></li> 209 209 </ul> 210 210 … … 217 217 <blockquote><pre>void main(char[][] args) { 218 218 auto parser = new OptionParser(); 219 parser.add _option(["--enable"], "flag", Action.StoreConst, "on");220 parser.add _option(["--disable"], "flag", Action.StoreConst, "off");221 auto options = parser.parse _args(args);219 parser.addOption(["--enable"], "flag", Action.StoreConst, "on"); 220 parser.addOption(["--disable"], "flag", Action.StoreConst, "off"); 221 auto options = parser.parse(args); 222 222 writefln("flag: %s", options["flag"]); 223 223 }</pre></blockquote> … … 237 237 <h4>Methods</h4> 238 238 <ul> 239 <li><tt>Option add _option(char[][] options, Action action, char[] const_value)</tt></li>240 <li><tt>Option add _option(char[][] options, char[] name, Action action, char[] const_value)</tt></li>239 <li><tt>Option addOption(char[][] options, Action action, char[] const_value)</tt></li> 240 <li><tt>Option addOption(char[][] options, char[] name, Action action, char[] const_value)</tt></li> 241 241 </ul> 242 242 … … 247 247 <blockquote><pre>void main(char[][] args) { 248 248 auto parser = new OptionParser(); 249 parser.add _option(["--blue"], "color", Action.AppendConst, "blue");250 parser.add _option(["--green"], "color", Action.AppendConst, "green");251 auto options = parser.parse _args(args);249 parser.addOption(["--blue"], "color", Action.AppendConst, "blue"); 250 parser.addOption(["--green"], "color", Action.AppendConst, "green"); 251 auto options = parser.parse(args); 252 252 writefln("colors: %s", options.list("color")); 253 253 }</pre></blockquote> … … 265 265 <h4>Methods</h4> 266 266 <ul> 267 <li><tt>Option add _option(char[][] options, Action action)</tt></li>268 <li><tt>Option add _option(char[][] options, char[] name, Action action)</tt></li>267 <li><tt>Option addOption(char[][] options, Action action)</tt></li> 268 <li><tt>Option addOption(char[][] options, char[] name, Action action)</tt></li> 269 269 </ul> 270 270 … … 279 279 <blockquote><pre>void main(char[][] args) { 280 280 auto parser = new OptionParser(); 281 parser.add _option(["--enable"], "flag", Action.SetTrue);282 parser.add _option(["--disable"], "flag", Action.SetFalse);283 auto options = parser.parse _args(args);281 parser.addOption(["--enable"], "flag", Action.SetTrue); 282 parser.addOption(["--disable"], "flag", Action.SetFalse); 283 auto options = parser.parse(args); 284 284 writefln("flag: %s", options.flag("flag")); 285 285 }</pre></blockquote> … … 299 299 <h4>Methods</h4> 300 300 <ul> 301 <li><tt>Option add _option(char[][] options, Action action)</tt></li>302 <li><tt>Option add _option(char[][] options, char[] name, Action action)</tt></li>301 <li><tt>Option addOption(char[][] options, Action action)</tt></li> 302 <li><tt>Option addOption(char[][] options, char[] name, Action action)</tt></li> 303 303 </ul> 304 304 … … 311 311 <blockquote><pre>void main(char[][] args) { 312 312 auto parser = new OptionParser(); 313 parser.add _option(["-v", "--verbose"], "verbosity", Action.Count);314 auto options = parser.parse _args(args);313 parser.addOption(["-v", "--verbose"], "verbosity", Action.Count); 314 auto options = parser.parse(args); 315 315 writefln("verbosity: %s", options.count("verbosity")); 316 316 }</pre></blockquote> … … 332 332 <h4>Methods</h4> 333 333 <ul> 334 <li><tt>Option add _option(char[][] options, void delegate() dg)</tt></li>335 <li><tt>Option add _option(char[][] options, void delegate(char[]) dg)</tt></li>336 <li><tt>Option add _option(char[][] options, void delegate(int) dg)</tt></li>334 <li><tt>Option addOption(char[][] options, void delegate() dg)</tt></li> 335 <li><tt>Option addOption(char[][] options, void delegate(char[]) dg)</tt></li> 336 <li><tt>Option addOption(char[][] options, void delegate(int) dg)</tt></li> 337 337 </ul> 338 338 <h4>Results</h4> … … 342 342 <blockquote><pre>void main(char[][] args) { 343 343 auto parser = new OptionParser(); 344 parser.add _option(["--callback"], {344 parser.addOption(["--callback"], { 345 345 writefln("Callback encountered!"); 346 346 }); 347 auto options = parser.parse _args(args);347 auto options = parser.parse(args); 348 348 writefln("Done."); 349 349 }</pre></blockquote> … … 365 365 <dt><tt>Options</tt></dt> <dd>The current results of parsing the command-line, up to when the callback was encountered.</dd> 366 366 367 <dt><tt>inout char[][]</tt></dt> <dd>The args array. This is mostly identical to the array originally passed to <tt>parse _args</tt>, with the following differences:367 <dt><tt>inout char[][]</tt></dt> <dd>The args array. This is mostly identical to the array originally passed to <tt>parse</tt>, with the following differences: 368 368 <ol> 369 369 <li>The first element, with the executable name, has been removed.</li> … … 381 381 <h4>Methods</h4> 382 382 <ul> 383 <li><tt>Option add _option(char[][] options, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li>384 <li><tt>Option add _option(char[][] options, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li>385 <li><tt>Option add _option(char[][] options, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li>386 <li><tt>Option add _option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li>387 <li><tt>Option add _option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li>388 <li><tt>Option add _option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li>383 <li><tt>Option addOption(char[][] options, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> 384 <li><tt>Option addOption(char[][] options, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> 385 <li><tt>Option addOption(char[][] options, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> 386 <li><tt>Option addOption(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> 387 <li><tt>Option addOption(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> 388 <li><tt>Option addOption(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> 389 389 </ul> 390 390 <h4>Results</h4> … … 394 394 <blockquote><pre>void main(char[][] args) { 395 395 auto parser = new OptionParser(); 396 parser.add _option(396 parser.addOption( 397 397 ["--adv-callback"], 398 398 (Options o, inout char[][] a, inout int i, char[] name) { … … 401 401 } 402 402 ); 403 parser.add _option(["-c"], Action.Count);404 auto options = parser.parse _args(args);403 parser.addOption(["-c"], Action.Count); 404 auto options = parser.parse(args); 405 405 writefln("count: %s", options.count("c")); 406 406 }</pre></blockquote> … … 431 431 <p>Options are printed in the following form. Given these options:</p> 432 432 433 <blockquote><pre>parser.add _option("-f", "--file").help("The file to operate on.")434 parser.add _option(["--enable"], Action.SetTrue).help("Enables the option.")</pre></blockquote>433 <blockquote><pre>parser.addOption("-f", "--file").help("The file to operate on.") 434 parser.addOption(["--enable"], Action.SetTrue).help("Enables the option.")</pre></blockquote> 435 435 436 436 <p><tt>Help</tt> produces the following:</p> … … 441 441 <p>The <tt>help</tt> method of the <tt>Option</tt> class provides the description used in the listing. Notice also the "<tt>=FILE</tt>" in the first option above. This indicates that the option accepts an argument. If you wanted to have this say "<tt>=FILENAME</tt>" instead, you would instead define the <tt>--file</tt> option as:</p> 442 442 443 <blockquote<tt>parser.add _option("-f", "--file").help("The file to operate on.").argName("filename");</tt></blockquote>443 <blockquote<tt>parser.addOption("-f", "--file").help("The file to operate on.").argName("filename");</tt></blockquote> 444 444 445 445 <p>The <tt>argName</tt> method of <tt>Option</tt> class defines this string, which otherwise defaults to the option's name. Note that it always converts the argument's name to all-caps.</p> … … 447 447 <h4>Methods</h4> 448 448 <ul>
