p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.67k stars 244 forks source link

Unexpected behavior when using choices() #307

Closed justou closed 11 months ago

justou commented 11 months ago

For example

#include <iostream>
#include "argparse.hpp"

int main(int argc, char **argv) {
    argparse::ArgumentParser program("test");

    program.add_argument("--input")
            .default_value(std::string{"baz"})
            .choices("foo", "bar", "baz");

    program.add_argument("--value").scan<'i', int>().default_value(0);

    try {
        program.parse_args(argc, argv);
        auto input = program.get("input");
        auto value = program.get<int>("value");
        std::cout << input << std::endl;
        std::cout << value << std::endl;
    }
    catch (const std::exception &err) {
        std::cerr << err.what() << std::endl;
        std::cerr << program;
        std::exit(1);
    }
    return 0;
}

main.exe --value 1 --input foo will give the expected result, but main.exe --input foo --value 1 will complain: Invalid argument "--value" - allowed options: {foo, bar, baz} It seems the order of choices() on command line matters.

Environment: Windows 10, MSVC2019, argparse v3.0