Changeset 79

Show
Ignore:
Timestamp:
01/13/07 02:21:43 (2 years ago)
Author:
KirkMcDonald
Message:

Flag options and more powerful callbacks.

Files:

Legend:

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

    r78 r79  
    141141        } 
    142142    } 
    143 
     143    bool flag(char[] opt) { 
     144        char[][]* o = opt in opts; 
     145        if (o) { 
     146            return (*o)[0] == "1"; 
     147        } else { 
     148            return false; 
     149        } 
     150    } 
     151
     152 
     153// Options, args, this opt's index in args, name[, arg] 
     154alias void delegate(Options, inout char[][], inout int, char[], char[]) OptionCallbackFancyArg; 
     155alias void delegate(Options, inout char[][], inout int, char[], int)    OptionCallbackFancyInt; 
     156alias void delegate(Options, inout char[][], inout int, char[])         OptionCallbackFancy; 
    144157 
    145158alias void delegate(char[]) OptionCallbackArg; 
    146159alias void delegate(int)    OptionCallbackInt; 
    147 alias void delegate() OptionCallback; 
     160alias void delegate()       OptionCallback; 
    148161 
    149162/* 
     
    157170 * CallbackVoid: dg 
    158171*/ 
    159 enum Action { Store, StoreConst, Append, AppendConst, Count, Callback, Help } 
     172enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } 
    160173enum ArgType { None, String, Integer } 
    161174 
    162175ArgType defaultType(Action action) { 
    163176    switch (action) { 
    164         case Action.Store, Action.Append, Action.Callback
     177        case Action.Store, Action.Append, Action.Callback, Action.CallbackFancy
    165178            return ArgType.String; 
    166179            break; 
     
    180193    OptionCallbackInt int_callback; 
    181194    OptionCallback void_callback; 
     195 
     196    OptionCallbackFancyArg fancy_callback; 
     197    OptionCallbackFancyInt fancy_int_callback; 
     198    OptionCallbackFancy fancy_void_callback; 
    182199    char[] helptext; 
    183200    this( 
     
    186203        OptionCallbackArg dga, OptionCallback dg, 
    187204        OptionCallbackInt dgi, 
    188         char[] help 
     205        OptionCallbackFancyArg fdga, 
     206        OptionCallbackFancyInt fdgi, 
     207        OptionCallbackFancy fdg 
    189208    ) { 
    190209        this.shortopts = shorts; 
     
    194213        this.name = name; 
    195214        this.argname = toupper(name); 
    196         this.helptext = help; 
    197215 
    198216        // Perform sanity checks. 
     
    217235                    case ArgType.None: 
    218236                        assert(dg !is null); 
    219                 } 
    220                 break; 
     237                        break; 
     238                } 
     239                break; 
     240            case Action.CallbackFancy: 
     241                switch (type) { 
     242                    case ArgType.String: 
     243                        assert(fdga !is null); 
     244                        break; 
     245                    case ArgType.Integer: 
     246                        assert(fdgi !is null); 
     247                        break; 
     248                    case ArgType.None: 
     249                        assert(fdg !is null); 
     250                        break; 
     251                } 
    221252            default: 
    222253                break; 
     
    226257        this.int_callback = dgi; 
    227258        this.void_callback = dg; 
     259        this.fancy_callback = fdga; 
     260        this.fancy_int_callback = fdgi; 
     261        this.fancy_void_callback = fdg; 
    228262    } 
    229263    char[] toString() { 
     
    242276    } 
    243277    // Does whatever this option is supposed to do. 
    244     void perform_action(OptionParser parser, Options results, char[] arg) { 
     278    void perform_action(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { 
    245279        int i; 
    246280        if (this.type == ArgType.Integer) { 
     
    267301                ++results.counted_opts[name]; 
    268302                break; 
     303            case Action.SetTrue: 
     304                results.opts[name] = ["1"]; 
     305                break; 
     306            case Action.SetFalse: 
     307                results.opts[name] = ["0"]; 
     308                break; 
    269309            case Action.Callback: 
    270310                switch (type) { 
     
    277317                    case ArgType.None: 
    278318                        void_callback(); 
     319                        break; 
     320                } 
     321                break; 
     322            case Action.CallbackFancy: 
     323                switch (type) { 
     324                    case ArgType.String: 
     325                        fancy_callback(results, args, idx, name, arg); 
     326                        break; 
     327                    case ArgType.Integer: 
     328                        fancy_int_callback(results, args, idx, name, i); 
     329                        break; 
     330                    case ArgType.None: 
     331                        fancy_void_callback(results, args, idx, name); 
    279332                        break; 
    280333                } 
     
    462515                    arg = null; 
    463516                } 
    464                 match.perform_action(this, options, arg); 
     517                match.perform_action(this, options, args, i, arg); 
    465518            } else if (opt.startswith("-")) { 
    466519                if (opt.length >= 2) { 
     
    483536                            } else { 
    484537                                arg = toUTF8(opt32[j+1 .. $]); 
    485                                 match.perform_action(this, options, arg); 
     538                                match.perform_action(this, options, args, i, arg); 
    486539                                break; 
    487540                            } 
     
    489542                            arg = null; 
    490543                        } 
    491                         match.perform_action(this, options, arg); 
     544                        match.perform_action(this, options, args, i, arg); 
    492545                    } 
    493546                } else { 
     
    512565        return option; 
    513566    } 
    514     //                    options  action               name  type                              const_value  dga   dgv   dgi   help 
     567    //                    options  action             name  type                       const_value  dga   dgv   dgi   fdga  fdgi  fdg 
    515568    Option add_option(char[][] options ...) { 
    516         return add_option(options, Action.Store,        null, defaultType(Action.Store),        null,        null, null, null, null); 
     569        return add_option(options, Action.Store,      null, defaultType(Action.Store), null,        null, null, null, null, null, null); 
    517570    } 
    518571    Option add_option(char[][] options, char[] name) { 
    519         return add_option(options, Action.Store,        name, defaultType(Action.Store),        null,        null, null, null, null); 
     572        return add_option(options, Action.Store,      name, defaultType(Action.Store), null,        null, null, null, null, null, null); 
    520573    } 
    521574    Option add_option(char[][] options, Action action) { 
    522         return add_option(options, action,              null, defaultType(action),              null,        null, null, null, null); 
     575        return add_option(options, action,            null, defaultType(action),       null,        null, null, null, null, null, null); 
    523576    } 
    524577    Option add_option(char[][] options, ArgType type) { 
    525         return add_option(options, Action.Store,        null, type,                             null,        null, null, null, null); 
     578        return add_option(options, Action.Store,      null, type,                      null,        null, null, null, null, null, null); 
    526579    } 
    527580    Option add_option(char[][] options, Action action, ArgType type) { 
    528         return add_option(options, action,              null, type,                             null,        null, null, null, null); 
     581        return add_option(options, action,            null, type,                      null,        null, null, null, null, null, null); 
    529582    } 
    530583    Option add_option(char[][] options, char[] name, Action action) { 
    531         return add_option(options, action,              name, defaultType(action),              null,        null, null, null, null); 
     584        return add_option(options, action,            name, defaultType(action),       null,        null, null, null, null, null, null); 
    532585    } 
    533586    Option add_option(char[][] options, char[] name, Action action, ArgType type) { 
    534         return add_option(options, action,              name, type,                             null,        null, null, null, null); 
     587        return add_option(options, action,            name, type,                      null,        null, null, null, null, null, null); 
    535588    } 
    536589    Option add_option(char[][] options, Action action, char[] const_value) { 
    537         return add_option(options, action,              null, defaultType(action),              const_value, null, null, null, null); 
     590        return add_option(options, action,            null, defaultType(action),       const_value, null, null, null, null, null, null); 
    538591    } 
    539592    Option add_option(char[][] options, char[] name, char[] const_value) { 
    540         return add_option(options, Action.StoreConst,   name, defaultType(Action.Store),        const_value, null, null, null, null); 
     593        return add_option(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null, null, null); 
    541594    } 
    542595    Option add_option(char[][] options, char[] name, Action action, char[] const_value) { 
    543         return add_option(options, action,              name, defaultType(action),              const_value, null, null, null, null); 
     596        return add_option(options, action,            name, defaultType(action),       const_value, null, null, null, null, null, null); 
    544597    } 
    545598    Option add_option(char[][] options, OptionCallbackArg dg) { 
    546         return add_option(options, Action.Callback,     null, defaultType(Action.Callback),     null,        dg,  null, null, null); 
     599        return add_option(options, Action.Callback,   null, ArgType.String,            null,        dg,   null, null, null, null, null); 
    547600    } 
    548601    Option add_option(char[][] options, OptionCallback dg) { 
    549         return add_option(options, Action.Callback,     null, ArgType.None,                     null,        null, dg,  null, null); 
     602        return add_option(options, Action.Callback,   null, ArgType.None,              null,        null, dg,   null, null, null, null); 
    550603    } 
    551604    Option add_option(char[][] options, OptionCallbackInt dg) { 
    552         return add_option(options, Action.Callback,     null, ArgType.Integer,                  null,        null, null, dg,   null); 
     605        return add_option(options, Action.Callback,   null, ArgType.Integer,           null,        null, null, dg,   null, null, null); 
     606    } 
     607    Option add_option(char[][] options, OptionCallbackFancyArg dg) { 
     608        return add_option(options, Action.CallbackFancy, null, ArgType.String,         null,        null, null, null, dg,   null, null); 
     609    } 
     610    Option add_option(char[][] options, OptionCallbackFancy dg) { 
     611        return add_option(options, Action.CallbackFancy, null, ArgType.None,           null,        null, null, null, null, null, dg); 
     612    } 
     613    Option add_option(char[][] options, OptionCallbackFancyInt dg) { 
     614        return add_option(options, Action.CallbackFancy, null, ArgType.Integer,        null,        null, null, null, null, dg,   null); 
     615    } 
     616    Option add_option(char[][] options, char[] name, OptionCallbackFancyArg dg) { 
     617        return add_option(options, Action.CallbackFancy, name, ArgType.String,         null,        null, null, null, dg,   null, null); 
     618    } 
     619    Option add_option(char[][] options, char[] name, OptionCallbackFancy dg) { 
     620        return add_option(options, Action.CallbackFancy, name, ArgType.None,           null,        null, null, null, null, null, dg); 
     621    } 
     622    Option add_option(char[][] options, char[] name, OptionCallbackFancyInt dg) { 
     623        return add_option(options, Action.CallbackFancy, name, ArgType.Integer,        null,        null, null, null, null, dg,   null); 
    553624    } 
    554625    // Although users certainly /can/ call this, all those overloads are there 
     
    559630        OptionCallbackArg callback, OptionCallback vcall, 
    560631        OptionCallbackInt icall, 
    561         char[] help 
     632        OptionCallbackFancyArg fdga, 
     633        OptionCallbackFancyInt fdgi, 
     634        OptionCallbackFancy fdg 
    562635    ) { 
    563636        char[][] shortopts; 
     
    599672                ); 
    600673        } 
    601         option = new Option(shortopts, longopts, type, action, name, const_value, callback, vcall, icall, help); 
     674        option = new Option(shortopts, longopts, type, action, name, const_value, callback, vcall, icall, fdga, fdgi, fdg); 
    602675        this.options ~= option; 
    603676        return option; 
  • misc/opttest.d

    r76 r79  
    99    writefln("value:       ", options.value("value")); 
    1010    writefln("number list: ", options.value_list("list")); 
     11    writefln("switch:      ", options.flag("switch")); 
    1112    writefln("args:        ", options.args); 
    1213} 
     
    2223        writefln("number callback: %d * 2 = %d", i, i*2); 
    2324    }).help("Calls a callback with a number."); 
     25    // A fancy callback 
     26    parser.add_option(["--fancy"], "a callback", (Options opts, inout char[][] a, inout int i, char[] name) { 
     27        writefln("Current file: %s", opts["file"]); 
     28        writefln("Remaining args: %s", a[i+1 .. $]); 
     29        // Skip the next arg 
     30        ++i; 
     31        writefln("This arg's name: %s", name); 
     32    }).help("Tests optparse's fancy callbacks."); 
     33    parser.add_option(["--enable"], "switch", Action.SetTrue).help("Enables the switch."); 
     34    parser.add_option(["--disable"], "switch", Action.SetFalse).help("Disables the switch."); 
    2435    parser.add_option(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER"); 
    2536    parser.add_option(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER");