Changeset 98

Show
Ignore:
Timestamp:
02/07/07 19:30:19 (2 years ago)
Author:
KirkMcDonald
Message:

Added default argument features to Tango version of optparse.

Files:

Legend:

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

    r84 r98  
    3131import tango.text.Ascii : toUpper; 
    3232import tango.stdc.stdlib : exit, EXIT_FAILURE, EXIT_SUCCESS; 
    33 import tango.text.convert.Integer : parse, toInt
     33import tango.text.convert.Integer : parse, toInt, toString = toUtf8
    3434import tango.text.convert.Utf : toUtf8, toUtf32; 
    3535 
     
    212212    char[] name, argname; 
    213213    char[] const_value; 
     214 
     215    char[] default_string; 
     216    int default_value; 
     217    bool default_flag, has_default; 
     218 
    214219    OptionCallbackArg callback; 
    215220    OptionCallbackInt int_callback; 
     
    235240        this.name = name; 
    236241        this.argname = toUpper(name.dup); 
     242        this.default_string = ""; 
     243        this.default_value = 0; 
     244        this.default_flag = false; 
    237245 
    238246        // Perform sanity checks. 
     
    297305        return result; 
    298306    } 
     307    //enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } 
     308    void issue_default(Options results) { 
     309        // Only set the default if the option doesn't already have a value. 
     310        char[][]* val = this.name in results.opts; 
     311        switch (this.action) { 
     312            case Action.Store, Action.Append: 
     313                if (val !is null) return; 
     314                if (this.type == ArgType.String) { 
     315                    results.opts[name] = [default_string]; 
     316                } else { 
     317                    results.opts[name] = [.toString(default_value)]; 
     318                } 
     319                break; 
     320            case Action.StoreConst, Action.AppendConst: 
     321                if (val !is null) return; 
     322                results.opts[name] = [default_string]; 
     323                break; 
     324            case Action.SetTrue, Action.SetFalse: 
     325                if (val !is null) return; 
     326                if (default_flag) { 
     327                    results.opts[name] = ["1"]; 
     328                } else { 
     329                    results.opts[name] = ["0"]; 
     330                } 
     331                break; 
     332            default: 
     333                return; 
     334        } 
     335    } 
    299336    // Does whatever this option is supposed to do. 
    300337    void performAction(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { 
     
    373410    Option argName(char[] argname) { 
    374411        this.argname = argname; 
     412        return this; 
     413    } 
     414    Option def(char[] val) { 
     415        if ( 
     416            (this.type != ArgType.String || (this.action != Action.Store && this.action != Action.Append)) && 
     417            this.action != Action.StoreConst && this.action != Action.AppendConst 
     418        ) 
     419            throw new OptionError("Cannot specify string default for non-string option '"~this.name~"'"); 
     420        this.has_default = true; 
     421        this.default_string = val; 
     422        return this; 
     423    } 
     424    Option def(int val) { 
     425        if (this.type != ArgType.Integer || (this.action != Action.Store && this.action != Action.Append)) 
     426            throw new OptionError("Cannot specify integer default for non-integer option '"~this.name~"'"); 
     427        this.has_default = true; 
     428        this.default_value = val; 
     429        return this; 
     430    } 
     431    Option def(bool val) { 
     432        if (this.action != Action.SetTrue && this.action != Action.SetFalse) 
     433            throw new OptionError("Cannot specify boolean default for non-flag option '"~this.name~"'"); 
     434        this.has_default = true; 
     435        this.default_flag = val; 
    375436        return this; 
    376437    } 
     
    606667                } 
    607668            } 
     669        } 
     670        foreach (o; this.options) { 
     671            o.issue_default(options); 
    608672        } 
    609673        return options;