Changeset 84

Show
Ignore:
Timestamp:
01/14/07 06:20:39 (2 years ago)
Author:
KirkMcDonald
Message:

Added doc-comments, changed to Tango naming conventions.

Files:

Legend:

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

    r83 r84  
    2222/** 
    2323 * Command-line option parsing, in the style of Python's optparse. 
     24 * 
     25 * Refer to the complete docs for more information. 
    2426 */ 
    2527module optparse; 
     
    8385} 
    8486 
    85 // Thrown if client code tries to set up an improper option. 
     87/// Thrown if client code tries to set up an improper option. 
    8688class OptionError : Exception { 
    8789    this(char[] msg) { super(msg); } 
     
    9294} 
    9395 
     96/++ 
     97This class represents the results after parsing the command-line. 
     98+/ 
    9499class Options { 
    95100    char[][][char[]] opts; 
    96101    int[char[]] counted_opts; 
     102    /// By default, leftover arguments are placed in this array. 
    97103    char[][] args; 
    98104 
     105    /// Retrieves the results of the Store and StoreConst actions. 
    99106    char[] opIndex(char[] opt) { 
    100107        char[][]* o = opt in opts; 
     
    105112        } 
    106113    } 
     114    /// Retrieves the results of the Store action, when the type is Integer. 
    107115    int value(char[] opt) { 
    108116        char[][]* o = opt in opts; 
     
    113121        } 
    114122    } 
     123    /// Retrieves the results of the Append and AppendConst actions. 
    115124    char[][] list(char[] opt) { 
    116125        char[][]* o = opt in opts; 
     
    121130        } 
    122131    } 
    123     int[] value_list(char[] opt) { 
     132    /// Retrieves the results of the Append action, when the type is Integer. 
     133    int[] valueList(char[] opt) { 
    124134        char[][]* o = opt in opts; 
    125135        int[] l; 
     
    132142        return l; 
    133143    } 
     144    /// Retrieves the results of the Count action. 
    134145    int count(char[] opt) { 
    135146        int* c = opt in counted_opts; 
     
    140151        } 
    141152    } 
     153    /// Retrieves the results of the SetTrue and SetFalse actions. 
    142154    bool flag(char[] opt) { 
    143155        char[][]* o = opt in opts; 
     
    151163 
    152164// Options, args, this opt's index in args, name[, arg] 
     165/// 
    153166alias void delegate(Options, inout char[][], inout int, char[], char[]) OptionCallbackFancyArg; 
     167/// 
    154168alias void delegate(Options, inout char[][], inout int, char[], int)    OptionCallbackFancyInt; 
     169/// 
    155170alias void delegate(Options, inout char[][], inout int, char[])         OptionCallbackFancy; 
    156171 
     172/// 
    157173alias void delegate(char[]) OptionCallbackArg; 
     174/// 
    158175alias void delegate(int)    OptionCallbackInt; 
     176/// 
    159177alias void delegate()       OptionCallback; 
    160178 
     
    169187 * CallbackVoid: dg 
    170188*/ 
    171 enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } 
    172 enum ArgType { None, String, Integer } 
     189/// 
     190enum Action { /+++/Store, /+++/StoreConst, /+++/Append, /+++/AppendConst, /+++/Count, /+++/SetTrue, /+++/SetFalse, /+++/Callback, /+++/CallbackFancy, /+++/Help /+++/} 
     191/// 
     192enum ArgType { /+++/None, /+++/String, /+++/Integer /+++/} 
    173193 
    174194ArgType defaultType(Action action) { 
     
    183203} 
    184204 
     205/++ 
     206This class represents a single command-line option. 
     207+/ 
    185208class Option { 
    186209    char[][] shortopts, longopts; 
     
    275298    } 
    276299    // Does whatever this option is supposed to do. 
    277     void perform_action(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { 
     300    void performAction(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { 
    278301        int i; 
    279302        if (this.type == ArgType.Integer) { 
     
    333356                break; 
    334357            case Action.Help: 
    335                 parser.help_text(); 
     358                parser.helpText(); 
    336359                exit(EXIT_SUCCESS); 
    337360                break; 
    338361        } 
    339362    } 
     363    /// Returns whether this option accepts an argument. 
    340364    bool hasArg() { 
    341365        return this.type != ArgType.None; 
    342366    } 
     367    /// Sets the help text for this option. 
    343368    Option help(char[] help) { 
    344369        this.helptext = help; 
    345370        return this; 
    346371    } 
     372    /// Sets the name of this option's argument, if it has one. 
    347373    Option argName(char[] argname) { 
    348374        this.argname = argname; 
     
    376402} 
    377403 
     404/++ 
     405This class is used to define a set of options, and parse the command-line 
     406arguments. 
     407+/ 
    378408class OptionParser { 
    379409    OptionCallbackArg leftover_cb; 
     410    /// An array of all of the options known by this parser. 
    380411    Option[] options; 
    381     char[] name, desc, argdesc; 
     412    char[] name, desc; 
     413    /// The description of the programs arguments, as used in the Help action. 
     414    char[] argdesc; 
    382415    private void delegate(char[]) error_callback; 
    383416 
     
    388421    } 
    389422 
    390     void set_error_callback(void delegate(char[]) dg) { 
     423    /// Sets a callback, to override the default error behavior. 
     424    void setErrorCallback(void delegate(char[]) dg) { 
    391425        error_callback = dg; 
    392426    } 
    393     void unknown_opt_error(char[] opt) { 
     427    void unknownOptError(char[] opt) { 
    394428        error("Unknown argument '"~opt~"'"); 
    395429    } 
    396     void expected_arg_error(char[] opt) { 
     430    void expectedArgError(char[] opt) { 
    397431        error("'"~opt~"' option expects an argument."); 
    398432    } 
     433    /// Displays an error message and terminates the program. 
    399434    void error(char[] err) { 
    400435        if (error_callback !is null) { 
    401436            error_callback(err); 
    402437        } else { 
    403             this.help_text(); 
     438            this.helpText(); 
    404439            Stdout.println(err); 
    405440        } 
     
    409444        int i; 
    410445        uint ate; 
    411         i = parse(s, 10u, &ate); 
     446        i = .parse(s, 10u, &ate); 
    412447        if (ate != s.length) 
    413448            error("Could not convert '"~s~"' to an integer."); 
     
    415450    } 
    416451 
    417     void help_text() { 
     452    /// Displays useful "help" information about the program's options. 
     453    void helpText() { 
    418454        int optWidth; 
    419455        char[][] optStrs; 
     
    456492        return path[idx+1 .. $]; 
    457493    } 
    458     char[] get_program_name(char[] path) { 
     494    char[] getProgramName(char[] path) { 
    459495        version(Windows) { 
    460496            // (Unicode note: ".exe" only contains 4 code units, so this slice 
     
    467503        return getBaseName(path); 
    468504    } 
    469     Options parse_args(char[][] args) { 
    470         this.name = get_program_name(args[0]); 
     505    /// Parses the passed command-line arguments and returns the results. 
     506    Options parse(char[][] args) { 
     507        this.name = getProgramName(args[0]); 
    471508        args = args[1 .. $]; 
    472509        Options options = new Options; 
     
    521558                match = matches(newopt); 
    522559                if (match is null) { 
    523                     unknown_opt_error(newopt); 
     560                    unknownOptError(newopt); 
    524561                } 
    525562                if (match.hasArg) { 
    526                     if (i == args.length-1) expected_arg_error(match.name); 
     563                    if (i == args.length-1) expectedArgError(match.name); 
    527564                    arg = args[i+1]; 
    528565                    ++i; 
     
    530567                    arg = null; 
    531568                } 
    532                 match.perform_action(this, options, args, i, arg); 
     569                match.performAction(this, options, args, i, arg); 
    533570            } else if (opt.startswith("-")) { 
    534571                if (opt.length >= 2) { 
     
    538575                        match = matches(newopt); 
    539576                        if (match is null) { 
    540                             unknown_opt_error(newopt); 
     577                            unknownOptError(newopt); 
    541578                        } 
    542579                        if (match.hasArg) { 
     
    544581                            // next element of args for the arg. 
    545582                            if (j == opt32.length-1) { 
    546                                 if (i == args.length-1) expected_arg_error(match.name); 
     583                                if (i == args.length-1) expectedArgError(match.name); 
    547584                                arg = args[i+1]; 
    548585                                ++i; 
     
    551588                            } else { 
    552589                                arg = .toUtf8(opt32[j+1 .. $]); 
    553                                 match.perform_action(this, options, args, i, arg); 
     590                                match.performAction(this, options, args, i, arg); 
    554591                                break; 
    555592                            } 
     
    557594                            arg = null; 
    558595                        } 
    559                         match.perform_action(this, options, args, i, arg); 
     596                        match.performAction(this, options, args, i, arg); 
    560597                    } 
    561598                } else { 
    562                     unknown_opt_error(opt); 
     599                    unknownOptError(opt); 
    563600                } 
    564601            } else { 
     
    573610    } 
    574611 
    575     void leftover_callback(OptionCallbackArg dg) { 
     612    /++ 
     613    Overrides the default behavior of leftover arguments, calling this callback 
     614    with them instead of adding them an array. 
     615    +/ 
     616    void leftoverCallback(OptionCallbackArg dg) { 
    576617        this.leftover_cb = dg; 
    577618    } 
    578     Option add_option(Option option) { 
     619    /// 
     620    Option addOption(Option option) { 
    579621        this.options ~= option; 
    580622        return option; 
    581623    } 
    582624    //                    options  action             name  type                       const_value  dga   dgv   dgi   fdga  fdgi  fdg 
    583     Option add_option(char[][] options ...) { 
    584         return add_option(options, Action.Store,      null, defaultType(Action.Store), null,        null, null, null, null, null, null); 
    585     } 
    586     Option add_option(char[][] options, char[] name) { 
    587         return add_option(options, Action.Store,      name, defaultType(Action.Store), null,        null, null, null, null, null, null); 
    588     } 
    589     Option add_option(char[][] options, Action action) { 
    590         return add_option(options, action,            null, defaultType(action),       null,        null, null, null, null, null, null); 
    591     } 
    592     Option add_option(char[][] options, ArgType type) { 
    593         return add_option(options, Action.Store,      null, type,                      null,        null, null, null, null, null, null); 
    594     } 
    595     Option add_option(char[][] options, Action action, ArgType type) { 
    596         return add_option(options, action,            null, type,                      null,        null, null, null, null, null, null); 
    597     } 
    598     Option add_option(char[][] options, char[] name, Action action) { 
    599         return add_option(options, action,            name, defaultType(action),       null,        null, null, null, null, null, null); 
    600     } 
    601     Option add_option(char[][] options, char[] name, Action action, ArgType type) { 
    602         return add_option(options, action,            name, type,                      null,        null, null, null, null, null, null); 
    603     } 
    604     Option add_option(char[][] options, Action action, char[] const_value) { 
    605         return add_option(options, action,            null, defaultType(action),       const_value, null, null, null, null, null, null); 
    606     } 
    607     Option add_option(char[][] options, char[] name, char[] const_value) { 
    608         return add_option(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null, null, null); 
    609     } 
    610     Option add_option(char[][] options, char[] name, Action action, char[] const_value) { 
    611         return add_option(options, action,            name, defaultType(action),       const_value, null, null, null, null, null, null); 
    612     } 
    613     Option add_option(char[][] options, OptionCallbackArg dg) { 
    614         return add_option(options, Action.Callback,   null, ArgType.String,            null,        dg,   null, null, null, null, null); 
    615     } 
    616     Option add_option(char[][] options, OptionCallback dg) { 
    617         return add_option(options, Action.Callback,   null, ArgType.None,              null,        null, dg,   null, null, null, null); 
    618     } 
    619     Option add_option(char[][] options, OptionCallbackInt dg) { 
    620         return add_option(options, Action.Callback,   null, ArgType.Integer,           null,        null, null, dg,   null, null, null); 
    621     } 
    622     Option add_option(char[][] options, OptionCallbackFancyArg dg) { 
    623         return add_option(options, Action.CallbackFancy, null, ArgType.String,         null,        null, null, null, dg,   null, null); 
    624     } 
    625     Option add_option(char[][] options, OptionCallbackFancy dg) { 
    626         return add_option(options, Action.CallbackFancy, null, ArgType.None,           null,        null, null, null, null, null, dg); 
    627     } 
    628     Option add_option(char[][] options, OptionCallbackFancyInt dg) { 
    629         return add_option(options, Action.CallbackFancy, null, ArgType.Integer,        null,        null, null, null, null, dg,   null); 
    630     } 
    631     Option add_option(char[][] options, char[] name, OptionCallbackFancyArg dg) { 
    632         return add_option(options, Action.CallbackFancy, name, ArgType.String,         null,        null, null, null, dg,   null, null); 
    633     } 
    634     Option add_option(char[][] options, char[] name, OptionCallbackFancy dg) { 
    635         return add_option(options, Action.CallbackFancy, name, ArgType.None,           null,        null, null, null, null, null, dg); 
    636     } 
    637     Option add_option(char[][] options, char[] name, OptionCallbackFancyInt dg) { 
    638         return add_option(options, Action.CallbackFancy, name, ArgType.Integer,        null,        null, null, null, null, dg,   null); 
     625    /// 
     626    Option addOption(char[][] options ...) { 
     627        return addOption(options, Action.Store,      null, defaultType(Action.Store), null,        null, null, null, null, null, null); 
     628    } 
     629    /// 
     630    Option addOption(char[][] options, char[] name) { 
     631        return addOption(options, Action.Store,      name, defaultType(Action.Store), null,        null, null, null, null, null, null); 
     632    } 
     633    /// 
     634    Option addOption(char[][] options, Action action) { 
     635        return addOption(options, action,            null, defaultType(action),       null,        null, null, null, null, null, null); 
     636    } 
     637    /// 
     638    Option addOption(char[][] options, ArgType type) { 
     639        return addOption(options, Action.Store,      null, type,                      null,        null, null, null, null, null, null); 
     640    } 
     641    /// 
     642    Option addOption(char[][] options, Action action, ArgType type) { 
     643        return addOption(options, action,            null, type,                      null,        null, null, null, null, null, null); 
     644    } 
     645    /// 
     646    Option addOption(char[][] options, char[] name, Action action) { 
     647        return addOption(options, action,            name, defaultType(action),       null,        null, null, null, null, null, null); 
     648    } 
     649    /// 
     650    Option addOption(char[][] options, char[] name, Action action, ArgType type) { 
     651        return addOption(options, action,            name, type,                      null,        null, null, null, null, null, null); 
     652    } 
     653    /// 
     654    Option addOption(char[][] options, Action action, char[] const_value) { 
     655        return addOption(options, action,            null, defaultType(action),       const_value, null, null, null, null, null, null); 
     656    } 
     657    /// 
     658    Option addOption(char[][] options, char[] name, char[] const_value) { 
     659        return addOption(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null, null, null); 
     660    } 
     661    /// 
     662    Option addOption(char[][] options, char[] name, Action action, char[] const_value) { 
     663        return addOption(options, action,            name, defaultType(action),       const_value, null, null, null, null, null, null); 
     664    } 
     665    /// 
     666    Option addOption(char[][] options, OptionCallbackArg dg) { 
     667        return addOption(options, Action.Callback,   null, ArgType.String,            null,        dg,   null, null, null, null, null); 
     668    } 
     669    /// 
     670    Option addOption(char[][] options, OptionCallback dg) { 
     671        return addOption(options, Action.Callback,   null, ArgType.None,              null,        null, dg,   null, null, null, null); 
     672    } 
     673    /// 
     674    Option addOption(char[][] options, OptionCallbackInt dg) { 
     675        return addOption(options, Action.Callback,   null, ArgType.Integer,           null,        null, null, dg,   null, null, null); 
     676    } 
     677    /// 
     678    Option addOption(char[][] options, OptionCallbackFancyArg dg) { 
     679        return addOption(options, Action.CallbackFancy, null, ArgType.String,         null,        null, null, null, dg,   null, null); 
     680    } 
     681    /// 
     682    Option addOption(char[][] options, OptionCallbackFancy dg) { 
     683        return addOption(options, Action.CallbackFancy, null, ArgType.None,           null,        null, null, null, null, null, dg); 
     684    } 
     685    /// 
     686    Option addOption(char[][] options, OptionCallbackFancyInt dg) { 
     687        return addOption(options, Action.CallbackFancy, null, ArgType.Integer,        null,        null, null, null, null, dg,   null); 
     688    } 
     689    /// 
     690    Option addOption(char[][] options, char[] name, OptionCallbackFancyArg dg) { 
     691        return addOption(options, Action.CallbackFancy, name, ArgType.String,         null,        null, null, null, dg,   null, null); 
     692    } 
     693    /// 
     694    Option addOption(char[][] options, char[] name, OptionCallbackFancy dg) { 
     695        return addOption(options, Action.CallbackFancy, name, ArgType.None,           null,        null, null, null, null, null, dg); 
     696    } 
     697    /// 
     698    Option addOption(char[][] options, char[] name, OptionCallbackFancyInt dg) { 
     699        return addOption(options, Action.CallbackFancy, name, ArgType.Integer,        null,        null, null, null, null, dg,   null); 
    639700    } 
    640701    // Although users certainly /can/ call this, all those overloads are there 
    641702    // for a reason. 
    642     Option add_option( 
     703    Option addOption( 
    643704        char[][] options,  
    644705        Action action, char[] name, ArgType type, char[] const_value, 
     
    684745            else 
    685746                throw new OptionError( 
    686                     "No options provided to add_option!" 
     747                    "No options provided to addOption!" 
    687748                ); 
    688749        } 
  • misc/tango/opttest.d

    r83 r84  
    2222    Stdout.formatln("verbosity:   {0}", options.count("verbose")); 
    2323    Stdout.formatln("value:       {0}", options.value("value")); 
    24     Stdout.formatln("number list: {0}", format_arr(options.value_list("list"))); 
     24    Stdout.formatln("number list: {0}", format_arr(options.valueList("list"))); 
    2525    Stdout.formatln("switch:      {0}", options.flag("switch")); 
    2626    Stdout.formatln("args:        {0}", format_arr(options.args)); 
     
    2929int main(char[][] args) { 
    3030    format = new Format!(char); 
    31     Stdout.println(format_arr(args)); 
     31    //Stdout.println(format_arr(args)); 
    3232    auto parser = new OptionParser("A test suite for optparser."); 
    33     parser.add_option("-f", "--file").help("Stores a single argument."); 
    34     parser.add_option(["-I", "--import"], "importPath", Action.Append).help("Adds arguments to a list."); 
    35     parser.add_option(["-c", "--callback"], { 
     33    parser.addOption("-f", "--file").help("Stores a single argument."); 
     34    parser.addOption(["-I", "--import"], "importPath", Action.Append).help("Adds arguments to a list."); 
     35    parser.addOption(["-c", "--callback"], { 
    3636        Stdout.formatln("callback encountered"); 
    3737    }).help("Calls a callback."); 
    38     parser.add_option(["-n", "--number"], (int i) { 
     38    parser.addOption(["-n", "--number"], (int i) { 
    3939        Stdout.formatln("number callback: {0} * 2 = {1}", i, i*2); 
    4040    }).help("Calls a callback with a number."); 
    4141    // A fancy callback 
    42     parser.add_option(["--fancy"], "a callback", (Options opts, inout char[][] a, inout int i, char[] name) { 
     42    parser.addOption(["--fancy"], "a callback", (Options opts, inout char[][] a, inout int i, char[] name) { 
    4343        Stdout.formatln("Current file: {0}", opts["file"]); 
    4444        Stdout.formatln("Remaining args: {0}", a[i+1 .. $]); 
     
    4747        Stdout.formatln("This arg's name: {0}", name); 
    4848    }).help("Tests optparse's fancy callbacks."); 
    49     parser.add_option(["--enable"], "switch", Action.SetTrue).help("Enables the switch."); 
    50     parser.add_option(["--disable"], "switch", Action.SetFalse).help("Disables the switch."); 
    51     parser.add_option(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER"); 
    52     parser.add_option(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER"); 
    53     parser.add_option(["-v", "--verbose"], Action.Count).help("Counts the number of times this option appears."); 
    54     parser.add_option(["-h", "--help"], Action.Help).help("Displays a help message."); 
    55     auto options = parser.parse_args(args); 
     49    parser.addOption(["--enable"], "switch", Action.SetTrue).help("Enables the switch."); 
     50    parser.addOption(["--disable"], "switch", Action.SetFalse).help("Disables the switch."); 
     51    parser.addOption(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER"); 
     52    parser.addOption(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER"); 
     53    parser.addOption(["-v", "--verbose"], Action.Count).help("Counts the number of times this option appears."); 
     54    parser.addOption(["-h", "--help"], Action.Help).help("Displays a help message."); 
     55    auto options = parser.parse(args); 
    5656 
    5757    print_opts(options);