docopt / docopt.cpp

C++11 port of docopt
Boost Software License 1.0
1.04k stars 146 forks source link

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

Open ericonr opened 2 years ago

ericonr commented 2 years ago
#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"
jaredgrubb commented 2 years ago

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?

ericonr commented 2 years ago

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.