p-ranav/argparse

Unexpected behavior when using choices()

justou opened this issue · 0 comments

justou commented

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