Changeset 93

Show
Ignore:
Timestamp:
02/04/07 00:56:59 (2 years ago)
Author:
KirkMcDonald
Message:

Default arguments.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • misc/optparse.d

    r82 r93  
    2626 
    2727import std.stdio : writefln, writef; 
    28 import std.string : find, toupper
     28import std.string : find, toupper, toString
    2929import std.c.stdlib : exit, EXIT_FAILURE, EXIT_SUCCESS; 
    3030import std.conv : toInt, ConvError; 
     
    180180    char[] name, argname; 
    181181    char[] const_value; 
     182 
     183    char[] default_string; 
     184    int default_value; 
     185    bool default_flag, has_default; 
     186 
    182187    OptionCallbackArg callback; 
    183188    OptionCallbackInt int_callback; 
     
    203208        this.name = name; 
    204209        this.argname = toupper(name); 
     210        this.default_string = ""; 
     211        this.default_value = 0; 
     212        this.default_flag = false; 
    205213 
    206214        // Perform sanity checks. 
     
    265273        return result; 
    266274    } 
     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    } 
    267304    // Does whatever this option is supposed to do. 
    268305    void perform_action(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { 
     
    338375    Option argName(char[] argname) { 
    339376        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; 
    340401        return this; 
    341402    } 
     
    552613                } 
    553614            } 
     615        } 
     616        foreach (o; this.options) { 
     617            o.issue_default(options); 
    554618        } 
    555619        return options; 
  • misc/optparse.html

    r82 r93  
    135135<blockquote><tt>int num = options.value("number");</tt></blockquote> 
    136136 
    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> 
    138138 
    139139<h4>Example</h4> 
     
    176176<blockquote><tt>int[] values = options.value_list("value");</tt></blockquote> 
    177177 
    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> 
    179179 
    180180<h4>Example</h4> 
     
    210210 
    211211<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> 
    213213 
    214214<p>At this time, only strings are supported as constant values.</p> 
     
    274274<blockquote><tt>bool flag = options.flag("flag");</tt></blockquote> 
    275275 
    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> 
    277277 
    278278<h4>Example</h4> 
     
    574574<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> 
    575575 
     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 
    576581<dt><tt>bool hasArg();</tt></dt> <dd>Returns whether this option accepts an argument.</dd> 
    577582 
  • misc/opttest.d

    r79 r93  
    1010    writefln("number list: ", options.value_list("list")); 
    1111    writefln("switch:      ", options.flag("switch")); 
     12    writefln("turn-off:    ", options.flag("turn")); 
     13    writefln("color:       ", options["color"]); 
    1214    writefln("args:        ", options.args); 
    1315} 
     
    3335    parser.add_option(["--enable"], "switch", Action.SetTrue).help("Enables the switch."); 
    3436    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"); 
    3539    parser.add_option(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER"); 
    3640    parser.add_option(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER");