Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This library is not "reentrable", instances of ArgumentParser are not reusable #141

Closed
KOLANICH opened this issue Oct 19, 2021 · 1 comment

Comments

@KOLANICH
Copy link
Contributor

ArgumentParser::parse_args cannot be used multiple times and it would fail to detect missingness of mandatory positional args. Because of that I have to split tests testing if mandatory positional args work correctly into multiple tests. It is not very convenient.

@skrobinson
Copy link
Contributor

Can you give an example of code that does not work as you expect?

The following code allows me to parse a new set of arguments, adding to already found arguments. Notice in the third parse that program has not forgotten about --config from the second parse.

#include <argparse/argparse.hpp>
#include <iostream>
#include <string>

int main(int argc, const char* argv[]) {
    argparse::ArgumentParser program("test");
    program.add_argument("--config");

    std::string value;
    try {
        program.parse_args({"test"});
        value = program.get("--config");
    } catch (const std::logic_error& err) {
        value = std::string("no argument");
    }
    std::cout << "1) config is '" << value << "'." << std::endl;

    try {
        program.parse_args({"test", "--config", "file.conf"});
        value = program.get("--config");
    } catch (const std::logic_error& err) {
        value = std::string("no argument");
    }
    std::cout << "2) config is '" << value << "'." << std::endl;

    try {
        program.parse_args({"test"});
        value = program.get("--config");
    } catch (const std::logic_error& err) {
        value = std::string("no argument");
    }
    std::cout << "3) config is '" << value << "'." << std::endl;
}
$ ./reuse-parse_args
1) config is 'no argument'.
2) config is 'file.conf'.
3) config is 'file.conf'.

@p-ranav p-ranav closed this as completed Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants