Changeset 126

Show
Ignore:
Timestamp:
08/16/07 18:20:08 (1 year ago)
Author:
KirkMcDonald
Message:

* Fixed default arguments in optparse.
* Added comboparse and combotest. (Will add docs later.)

Files:

Legend:

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

    r104 r126  
    3939 
    4040version (Tango) { 
     41    // Although I've placed these Tango imports here, Tango support is 
     42    // incomplete. 
    4143    import tango.io.FileSystem : FileSystem; 
    4244    import tango.io.FilePath : FilePath; 
     
    5557    import std.conv : toInt, toReal, ConvError; 
    5658    import std.path : getBaseName, getDirName, join, pathsep; 
    57     import std.c.stdlib : getenv, strlen; 
     59    import std.c.stdlib : getenv; 
     60    import std.c.string : strlen; 
    5861} 
    5962 
     
    132135            char[][] paths = split(env("PATH"), SystemPathSeparator); 
    133136            foreach (path; paths) { 
    134                 if (new FileProxy(bin_name.join(path)).isExisting()) 
     137                if ((new FileProxy(bin_name.join(path))).isExisting()) 
    135138                    return file.join(path).toUtf8(); 
    136139            } 
     
    228231    /// Requests if a section exists in the parser. 
    229232    bool has_section(char[] name) { 
    230         return name in config !is null; 
     233        return (name in config) !is null; 
    231234    } 
    232235    private void no_section(char[] section) { 
  • misc/optparse.d

    r125 r126  
    211211enum Action { /+++/Store, /+++/StoreConst, /+++/Append, /+++/AppendConst, /+++/Count, /+++/SetTrue, /+++/SetFalse, /+++/Callback, /+++/CallbackFancy, /+++/Help /+++/} 
    212212/// 
    213 enum ArgType { /+/+++/None,+/ /+++/String, /+++/Integer /+++/} 
     213enum ArgType { /+/+++/None,+/ /+++/String, /+++/Integer, /+++/Bool /+++/} 
    214214 
    215215/++ 
     
    275275    } 
    276276    //enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } 
     277    bool supports_default() { 
     278        return false; 
     279    } 
     280    ArgType def_type() { 
     281        throw new OptionError("Option "~name~" does not support default arguments."); 
     282    } 
     283    bool has_default() { 
     284        throw new OptionError("Option "~name~" does not support default arguments."); 
     285    } 
    277286    void issue_default(Options results) { 
    278287        return; 
     
    333342    static const ArgType default_type = ArgType.String; 
    334343    ArgType type; 
    335     bool has_default = false; 
     344    bool _has_default = false; 
    336345    char[] default_string; 
    337346    this(char[][] options, char[] name, ArgType type) { 
    338347        super(options, name); 
     348        if (type != ArgType.Integer && type != ArgType.String) { 
     349            throw new OptionError("Argument type for Store and Append must be Integer or String."); 
     350        } 
    339351        this.type = type; 
    340352    } 
     
    345357        } 
    346358    } 
     359    override ArgType def_type() { 
     360        return this.type; 
     361    } 
    347362    override Option def(char[] val) { 
    348363        if (this.type != ArgType.String) 
    349364            super.def(val); 
    350         this.has_default = true; 
     365        this._has_default = true; 
    351366        this.default_string = val; 
    352367        return this; 
     
    355370        if (this.type != ArgType.Integer) 
    356371            super.def(val); 
    357         this.has_default = true; 
     372        this._has_default = true; 
    358373        this.default_string = .toString(val); 
    359374        return this; 
    360375    } 
     376    override bool supports_default() { 
     377        return true; 
     378    } 
     379    override bool has_default() { 
     380        return _has_default; 
     381    } 
    361382    override void issue_default(Options results) { 
    362         if (has_default) results.storeArg(this.name, this.default_string); 
     383        if (_has_default) results.storeArg(this.name, this.default_string); 
    363384    } 
    364385    override bool hasArg() { 
     
    386407} 
    387408abstract class ConstOption : Option { 
    388     bool has_default = false; 
     409    bool _has_default = false; 
    389410    char[] default_value; 
    390411    char[] const_value; 
     
    394415    } 
    395416    override Option def(char[] val) { 
    396         this.has_default = true; 
     417        this._has_default = true; 
    397418        this.default_value = val; 
    398419        return this; 
    399420    } 
     421    override ArgType def_type() { 
     422        return ArgType.String; 
     423    } 
     424    override bool supports_default() { 
     425        return true; 
     426    } 
     427    override bool has_default() { 
     428        return _has_default; 
     429    } 
    400430    override void issue_default(Options results) { 
    401         if (has_default) results.storeArg(this.name, this.default_value); 
     431        if (_has_default) results.storeArg(this.name, this.default_value); 
    402432    } 
    403433} 
     
    419449} 
    420450abstract class SetBool : Option { 
    421     bool has_default = false; 
     451    bool _has_default = false; 
    422452    bool default_flag = false; 
    423453    this(char[][] options, char[] name) { 
     
    425455    } 
    426456    override Option def(bool val) { 
    427         this.has_default = true; 
     457        this._has_default = true; 
    428458        this.default_flag = val; 
    429459        return this; 
    430460    } 
     461    override ArgType def_type() { 
     462        return ArgType.Bool; 
     463    } 
     464    override bool supports_default() { 
     465        return true; 
     466    } 
     467    override bool has_default() { 
     468        return _has_default; 
     469    } 
    431470    override void issue_default(Options results) { 
    432         if (has_default) results.storeArg(this.name, this.default_flag); 
     471        if (_has_default) results.storeArg(this.name, this.default_flag); 
    433472    } 
    434473} 
     
    703742        int idx; 
    704743        Option match; 
     744 
     745        foreach (o; this.options) { 
     746            o.issue_default(options); 
     747        } 
    705748 
    706749        for (int i=0; i<args.length; ++i) { 
     
    778821            } 
    779822        } 
    780         foreach (o; this.options) { 
    781             o.issue_default(options); 
    782         } 
    783823        return options; 
    784824    }