Changeset 80

Show
Ignore:
Timestamp:
01/13/07 05:56:16 (2 years ago)
Author:
KirkMcDonald
Message:

Refactored error(). error() now calls help_text().

Files:

Legend:

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

    r79 r80  
    7272    if (s.length < end.length) return false; 
    7373    return s[$ - end.length .. $] == end; 
    74 } 
    75  
    76 int toOptInt(char[] s) { 
    77     int i; 
    78     try { 
    79         i = toInt(s); 
    80     } catch (ConvError e) { 
    81         error("Could not convert '"~s~"' to an integer."); 
    82     } 
    83     return i; 
    8474} 
    8575 
     
    280270        if (this.type == ArgType.Integer) { 
    281271            // Verify that it's an int. 
    282             i = toOptInt(arg); 
     272            i = parser.toOptInt(arg); 
    283273        } 
    284274        switch (this.action) { 
     
    377367} 
    378368 
    379 private void delegate(char[]) error_callback; 
    380 void set_error_callback(void delegate(char[]) dg) { 
    381     error_callback = dg; 
    382 } 
    383  
    384 void unknown_opt_error(char[] opt) { 
    385     error("Unknown argument '"~opt~"'"); 
    386 } 
    387 void expected_arg_error(char[] opt) { 
    388     error("'"~opt~"' option expects an argument."); 
    389 } 
    390 void error(char[] err) { 
    391     if (error_callback !is null) { 
    392         error_callback(err); 
    393     } else { 
    394         writefln(err); 
    395     } 
    396     exit(EXIT_FAILURE); 
    397 } 
    398  
    399369class OptionParser { 
    400370    OptionCallbackArg leftover_cb; 
    401371    Option[] options; 
    402372    char[] name, desc, argdesc; 
     373    private void delegate(char[]) error_callback; 
    403374 
    404375    this(char[] desc="") { 
     
    406377        this.desc = desc; 
    407378        this.argdesc = "[options] args..."; 
     379    } 
     380 
     381    void set_error_callback(void delegate(char[]) dg) { 
     382        error_callback = dg; 
     383    } 
     384    void unknown_opt_error(char[] opt) { 
     385        error("Unknown argument '"~opt~"'"); 
     386    } 
     387    void expected_arg_error(char[] opt) { 
     388        error("'"~opt~"' option expects an argument."); 
     389    } 
     390    void error(char[] err) { 
     391        if (error_callback !is null) { 
     392            error_callback(err); 
     393        } else { 
     394            this.help_text(); 
     395            writefln(err); 
     396        } 
     397        exit(EXIT_FAILURE); 
     398    } 
     399    int toOptInt(char[] s) { 
     400        int i; 
     401        try { 
     402            i = toInt(s); 
     403        } catch (ConvError e) { 
     404            error("Could not convert '"~s~"' to an integer."); 
     405        } 
     406        return i; 
    408407    } 
    409408