docopt/docopt.cpp

Using optional args out of order leads to wrong values all around

ericonr opened this issue · 2 comments

#include "docopt.h"
#include <iostream>

static const char usage[] =
R"(Usage:
    order [-t <vt> -C <vc>]

Options:
-t <vt>  T val
-C <vc>  C val
)";

int main(int argc, char *argv[])
{
    std::map<std::string, docopt::value> args = docopt::docopt(usage, {argv+1, argv+argc}, true);

    for(auto const& arg : args) {
        std::cout << arg.first << ": " << arg.second << std::endl;
    }
}

In same order as the help string (correct):

./order -t 0 -C 1
-C: true
-t: true
<vc>: "1"
<vt>: "0"

In opposite order (wrong):

./order -C 0 -t 1
-C: true
-t: true
<vc>: "1"
<vt>: "0"

Only the first one (correct):

./order -t 0
-C: false
-t: true
<vc>: null
<vt>: "0"

Only the second one (wrong):

./order -C 0
-C: true
-t: false
<vc>: null
<vt>: "0"

I think this is mostly behaving correctly -- as I don't think the original docopt in Python does this as well.

This would probably be pretty hard to add on. What is the use-case where this would be useful?

I'm not sure I understand your comment? As far as I can see it isn't a new feature, but a bug which has the parser all confused and putting the wrong things in the return map.