Thread overview
[Issue 16539] std.getopt should invoke callbacks in the order given on the command line
Oct 15, 2016
Jon Degenhardt
Dec 13, 2016
Jon Degenhardt
Oct 28, 2022
kinke
Oct 28, 2022
hsteoh@qfbox.info
Oct 28, 2022
kinke
Dec 17, 2022
Iain Buclaw
October 15, 2016
https://issues.dlang.org/show_bug.cgi?id=16539

--- Comment #1 from Jon Degenhardt <jrdemail2000-dlang@yahoo.com> ---
A similar example, but using an array shared by multiple command line options. The arrays values are populated in the order specified lexically in the code. It would be better if they were populated in the order specified at run-time on the command line.

===== fillorder.d =====

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

    string[] cmdvals;
    try {
        auto r = getopt(
            args,
            "b|bb", "VAL   Append VAL to cmdvals", &cmdvals,
            "a|aa", "VAL   Append VAL to cmdvals", &cmdvals,
            "c|cc", "VAL   Append VAL to cmdvals", &cmdvals,
            );

        if (r.helpWanted) {
            defaultGetoptPrinter(
                "std.getopt array fill order test. Use options multiple times
in different orders.",
                r.options);
            return;
        }
    } catch (Exception exc) {
        stderr.writeln("Error processing command line arguments: ", exc.msg);
        return;
    }

    writeln("cmdvals array: ", cmdvals);
}

==========================

$ dmd fillorder.d
$ ./fillorder -a 1 -b 2 -c 3 -a 4 -b 5 -c 6
cmdvals array: ["2", "5", "1", "4", "3", "6"]

--
December 13, 2016
https://issues.dlang.org/show_bug.cgi?id=16539

--- Comment #2 from Jon Degenhardt <jrdemail2000-dlang@yahoo.com> ---
I looked into adding support for processing arguments in command line (run-time) order. However, processing arguments in the compile-time specified order (getopt function call order) appears fairly baked into the design. My personal assessment is that it would be a fair undertaking. If this such an effort was done, it might be better to do it as part of a new version of getopt that was improved in other dimensions as well.

For my own code I created a wrapper over getopt that processes arguments in command line order. It works fine most of the time, but is more brittle/hacky than appropriate for phobos. The wrapper is here: https://github.com/eBay/tsv-utils-dlang/blob/master/common/src/getopt_inorder.d

--
March 25, 2018
https://issues.dlang.org/show_bug.cgi?id=16539

hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@quickfur.ath.cx

--
March 25, 2018
https://issues.dlang.org/show_bug.cgi?id=16539

--- Comment #3 from hsteoh@quickfur.ath.cx ---
Andrei has proposed adding std.getopt.config.commandLineOrder, which must be the first argument in the list, to switch to this behaviour.

Cf. https://forum.dlang.org/post/p96fmv$20m7$1@digitalmars.com

--
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=16539

kinke <kinke@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net
           Hardware|x86                         |All
                 OS|Mac OS X                    |All
           Severity|enhancement                 |critical

--- Comment #4 from kinke <kinke@gmx.net> ---
Just stumbled upon this, absolutely terrible.

--
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=16539

--- Comment #5 from hsteoh@qfbox.info ---
The last time I complained about this, Andrei said it's not important.  I didn't know what it would take to convince him, so I wrote my own getopt instead.  Maybe you'll have a better chance at changing this annoying behaviour?

Note that it *will* require extensive rewriting, because the current implementation is heavily keyed on this weird processing order, and it will require some non-trivial code surgery to refactor it to do things a different way.

--
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=16539

--- Comment #6 from kinke <kinke@gmx.net> ---
Well I'm not too keen on fixing it myself, but I have a use case (mutually-exclusive flags pair) which should make it pretty clear that it's a serious problem:

```
void main(string[] args) {
    import std.getopt;
    import std.typecons;

    Nullable!bool useGui;
    void handleCliOrGuiMode(string option) {
        useGui = (option == "gui");
    }

    getopt(args, "cli", &handleCliOrGuiMode, "gui", &handleCliOrGuiMode);

    import std.stdio;
    writeln("useGui: ", useGui);
}
```

```
$ dmd -run foo.d --gui --cli
useGui: true
```

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=16539

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P2

--
December 01
https://issues.dlang.org/show_bug.cgi?id=16539

--- Comment #7 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10201

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--