Changeset 75

Show
Ignore:
Timestamp:
01/09/07 20:41:38 (2 years ago)
Author:
KirkMcDonald
Message:

Integer argument support.

Files:

Legend:

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

    r74 r75  
    2828import std.string : split, find; 
    2929import std.c.stdlib : exit, EXIT_FAILURE; 
     30import std.conv : toInt, ConvError; 
    3031 
    3132/* 
     
    6768} 
    6869 
     70int toOptInt(char[] s) { 
     71    int i; 
     72    try { 
     73        i = toInt(s); 
     74    } catch (ConvError e) { 
     75        error("Could not convert '"~s~"' to an integer."); 
     76    } 
     77    return i; 
     78} 
     79 
     80// Thrown if client code tries to set up an improper option. 
    6981class OptionError : Exception { 
     82    this(char[] msg) { super(msg); } 
     83} 
     84// Thrown if client code tries to extract the wrong type from an option. 
     85class OptionTypeError : Exception { 
    7086    this(char[] msg) { super(msg); } 
    7187} 
     
    84100        } 
    85101    } 
     102    int value(char[] opt) { 
     103        char[][]* o = opt in opts; 
     104        if (o) { 
     105            return toInt((*o)[0]); 
     106        } else { 
     107            return 0; 
     108        } 
     109    } 
    86110    char[][] list(char[] opt) { 
    87111        char[][]* o = opt in opts; 
     
    92116        } 
    93117    } 
     118    int[] value_list(char[] opt) { 
     119        char[][]* o = opt in opts; 
     120        int[] l; 
     121        if (o) { 
     122            l.length = (*o).length; 
     123            foreach (i, s; *o) { 
     124                l[i] = toInt(s); 
     125            } 
     126        } 
     127        return l; 
     128    } 
    94129    int count(char[] opt) { 
    95130        int* c = opt in counted_opts; 
     
    103138 
    104139alias void delegate(char[]) OptionCallbackArg; 
     140alias void delegate(int)    OptionCallbackInt; 
    105141alias void delegate() OptionCallback; 
    106142 
     
    115151 * CallbackVoid: dg 
    116152*/ 
    117 enum Action { Store, StoreConst, Append, AppendConst, Count, CallbackVoid, CallbackArg, Help } 
     153enum Action { Store, StoreConst, Append, AppendConst, Count, Callback, Help } 
     154enum ArgType { None, String, Integer } 
     155 
     156ArgType defaultType(Action action) { 
     157    switch (action) { 
     158        case Action.Store, Action.Append, Action.Callback: 
     159            return ArgType.String; 
     160            break; 
     161        default: 
     162            return ArgType.None; 
     163            break; 
     164    } 
     165
    118166 
    119167class Option { 
    120168    char[][] shortopts, longopts; 
    121169    Action action; 
     170    ArgType type; 
    122171    char[] name; 
    123172    char[] const_value; 
    124173    OptionCallbackArg callback; 
     174    OptionCallbackInt int_callback; 
    125175    OptionCallback void_callback; 
    126176    this( 
    127         char[][] shorts, char[][] longs, 
     177        char[][] shorts, char[][] longs, ArgType type, 
    128178        Action act, char[] name, char[] const_value, 
    129         OptionCallbackArg dga, OptionCallback dg 
     179        OptionCallbackArg dga, OptionCallback dg, 
     180        OptionCallbackInt dgi 
    130181    ) { 
    131182        this.shortopts = shorts; 
    132183        this.longopts = longs; 
    133184        this.action = act; 
     185        this.type = type; 
    134186        this.name = name; 
    135187 
     
    138190        switch (act) { 
    139191            case Action.Store, Action.Append: 
     192                assert(type != ArgType.None); 
    140193                break; 
    141194            case Action.StoreConst, Action.AppendConst: 
     195                assert(type == ArgType.None); 
    142196                assert(const_value !is null); 
    143197                break; 
    144             case Action.CallbackArg: 
    145                 assert(dga !is null); 
    146                 break; 
    147             case Action.CallbackVoid: 
    148                 assert(dg !is null); 
    149                 break; 
    150             default: break; 
     198            case Action.Callback: 
     199                //assert(type != ArgType.None); 
     200                switch (type) { 
     201                    case ArgType.String: 
     202                        assert(dga !is null); 
     203                        break; 
     204                    case ArgType.Integer: 
     205                        assert(dgi !is null); 
     206                        break; 
     207                    case ArgType.None: 
     208                        assert(dg !is null); 
     209                } 
     210                break; 
     211            default: 
     212                break; 
    151213        } 
    152214        this.const_value = const_value; 
    153215        this.callback = dga; 
     216        this.int_callback = dgi; 
    154217        this.void_callback = dg; 
    155218    } 
    156219    // Does whatever this option is supposed to do. 
    157220    void perform_action(Options results, char[] arg) { 
    158         char[][]* these_args; 
     221        int i; 
     222        if (this.type == ArgType.Integer) { 
     223            // Verify that it's an int. 
     224            i = toOptInt(arg); 
     225        } 
    159226        switch (this.action) { 
    160227            case Action.Store: 
     
    166233            case Action.StoreConst: 
    167234                assert(arg is null, "Got unexpected argument for '"~name~"' option."); 
    168                 these_args = name in results.opts; 
    169                 if (these_args.length == 0) { 
    170                     (*these_args) ~= const_value; 
    171                 } 
     235                results.opts[name] = [const_value]; 
    172236                break; 
    173237            case Action.AppendConst: 
     
    179243                ++results.counted_opts[name]; 
    180244                break; 
    181             case Action.CallbackArg: 
    182                 callback(arg); 
    183                 break; 
    184             case Action.CallbackVoid: 
    185                 void_callback(); 
     245            case Action.Callback: 
     246                switch (type) { 
     247                    case ArgType.String: 
     248                        callback(arg); 
     249                        break; 
     250                    case ArgType.Integer: 
     251                        int_callback(i); 
     252                        break; 
     253                    case ArgType.None: 
     254                        void_callback(); 
     255                        break; 
     256                } 
    186257                break; 
    187258            case Action.Help: 
     
    191262    } 
    192263    bool hasArg() { 
    193         switch (this.action) { 
    194             case Action.Store, Action.Append, Action.CallbackArg: 
    195                 return true; 
    196             default: 
    197                 return false; 
    198         } 
     264        return this.type != ArgType.None; 
    199265    } 
    200266    // Returns true if the passed option string matches this option. 
     
    333399    } 
    334400 
     401    //             options  action               name  type                              const_value  dga   dgv   dgi 
    335402    void add_option(char[][] options ...) { 
    336         add_option(options, Action.Store, null, null, null, null); 
     403        add_option(options, Action.Store,        null, defaultType(Action.Store),        null,        null, null, null); 
    337404    } 
    338405    void add_option(char[][] options, char[] name) { 
    339         add_option(options, Action.Store, name, null, null, null); 
     406        add_option(options, Action.Store,        name, defaultType(Action.Store),        null,        null, null, null); 
    340407    } 
    341408    void add_option(char[][] options, Action action) { 
    342         add_option(options, action, null, null, null, null); 
     409        add_option(options, action,              null, defaultType(action),              null,        null, null, null); 
     410    } 
     411    void add_option(char[][] options, ArgType type) { 
     412        add_option(options, Action.Store,        null, type,                             null,        null, null, null); 
     413    } 
     414    void add_option(char[][] options, Action action, ArgType type) { 
     415        add_option(options, action,              null, type,                             null,        null, null, null); 
    343416    } 
    344417    void add_option(char[][] options, char[] name, Action action) { 
    345         add_option(options, action, name, null, null, null); 
     418        add_option(options, action,              name, defaultType(action),              null,        null, null, null); 
     419    } 
     420    void add_option(char[][] options, char[] name, Action action, ArgType type) { 
     421        add_option(options, action,              name, type,                             null,        null, null, null); 
     422    } 
     423    void add_option(char[][] options, Action action, char[] const_value) { 
     424        add_option(options, action,              null, defaultType(action),              const_value, null, null, null); 
     425    } 
     426    void add_option(char[][] options, char[] name, char[] const_value) { 
     427        add_option(options, Action.StoreConst,   name, defaultType(Action.Store),        const_value, null, null, null); 
     428    } 
     429    void add_option(char[][] options, char[] name, Action action, char[] const_value) { 
     430        add_option(options, action,              name, defaultType(action),              const_value, null, null, null); 
    346431    } 
    347432    void add_option(char[][] options, OptionCallbackArg dg) { 
    348         add_option(options, Action.CallbackArg, null, null, dg, null); 
     433        add_option(options, Action.Callback,     null, defaultType(Action.Callback),     null,        dg,   null, null); 
    349434    } 
    350435    void add_option(char[][] options, OptionCallback dg) { 
    351         add_option(options, Action.CallbackVoid, null, null, null, dg); 
    352     } 
     436        add_option(options, Action.Callback,     null, ArgType.None,                     null,        null, dg,   null); 
     437    } 
     438    void add_option(char[][] options, OptionCallbackInt dg) { 
     439        add_option(options, Action.Callback,     null, ArgType.Integer,                  null,        null, null, dg); 
     440    } 
     441    // Although users certainly /can/ call this, all those overloads are there 
     442    // for a reason. 
    353443    void add_option( 
    354444        char[][] options,  
    355         Action action, char[] name, char[] const_value, 
    356         OptionCallbackArg callback, OptionCallback vcall 
     445        Action action, char[] name, ArgType type, char[] const_value, 
     446        OptionCallbackArg callback, OptionCallback vcall, 
     447        OptionCallbackInt icall 
    357448    ) { 
    358449        char[][] shortopts; 
     
    383474                name = shortopts[0][1 .. 2]; 
    384475        } 
    385         this.options ~= new Option(shortopts, longopts, action, name, const_value, callback, vcall); 
    386     } 
    387 } 
    388  
     476        this.options ~= new Option(shortopts, longopts, type, action, name, const_value, callback, vcall, icall); 
     477    } 
     478} 
     479 
  • misc/opttest.d

    r74 r75  
    44 
    55void print_opts(Options options) { 
    6     writefln("file:      ", options["file"]); 
    7     writefln("imports:   ", options.list("importPath")); 
    8     writefln("verbosity: ", options.count("verbose")); 
     6    writefln("file:        ", options["file"]); 
     7    writefln("imports:     ", options.list("importPath")); 
     8    writefln("verbosity:   ", options.count("verbose")); 
     9    writefln("value:       ", options.value("value")); 
     10    writefln("number list: ", options.value_list("list")); 
    911    foreach (a; options.args) { 
    10         writefln("arg:       ", a); 
     12        writefln("arg:         ", a); 
    1113    } 
    1214} 
     
    1921        writefln("callback encountered"); 
    2022    }); 
     23    parser.add_option(["-n", "--number"], (int i) { 
     24        writefln("number callback: %d * 2 = %d", i, i*2); 
     25    }); 
     26    parser.add_option(["-V", "--value"], ArgType.Integer); 
     27    parser.add_option(["-l", "--list"], Action.Append, ArgType.Integer); 
    2128    parser.add_option(["-v", "--verbose"], Action.Count); 
    2229    auto options = parser.parse_args(args);