www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16539] New: std.getopt should invoke callbacks in the order


          Issue ID: 16539
           Summary: std.getopt should invoke callbacks in the order given
                    on the command line
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: jrdemail2000-dlang yahoo.com

std.getopt.getopt invokes callback functions in the order they listed in the
call to getopt. Instead callbacks should be invoked in the order used on the
command line at run-time. This would support use cases where the user's
argument order is taken into account. If there are also use cases where the
existing behavior is preferred then a config option can be used.

An example to illustrate. This program has three options with callbacks. The
callbacks simply print that it was called. The options can be passed any in any
order on the command, and any number of times. The callbacks are called in the
order listed in the code, rather than the order listed on the command line.

==== callorder.d ====

void main(string [] args)
    import std.getopt;
    import std.stdio;

    void optionHandler(string option, string optionVal)
        writefln("optionHander(%s, %s)", option, optionVal);

    try {
        auto r = getopt(
            "a|aa", "aaa val", &optionHandler,
            "b|bb", "bbb val", &optionHandler,
            "c|cc", "ccc val", &optionHandler,

        if (r.helpWanted) {
            auto helpText =
                "Option handler call order test. Use options multiple times in
different orders.";
            defaultGetoptPrinter(helpText, r.options);
    } catch (Exception exc) {
        stderr.writeln("Error proccessing command line arguments: ", exc.msg);


$ dmd callorder.d
$ ./callorder -a 1 -b 2 -c 3 --cc 4 --bb 5 --aa 6 -a 7 -b 8 -c 9
optionHander(a|aa, 1)
optionHander(a|aa, 6)
optionHander(a|aa, 7)
optionHander(b|bb, 2)
optionHander(b|bb, 5)
optionHander(b|bb, 8)
optionHander(c|cc, 3)
optionHander(c|cc, 4)
optionHander(c|cc, 9)

Sep 25 2016