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

Change parse_args readme and samples to catch const std::exception& #294

Merged

Conversation

cobyj33
Copy link
Contributor

@cobyj33 cobyj33 commented Oct 26, 2023

parse_args can throw exceptions that are not based on std::runtime_error, and the error message should show on all errors.

For example, if a user is using a program formatted as:

parser.add_argument("number")
    .help("Enter a number")
    .scan<'i', int>();

try {
    parser.parse_args(argc, argv);
} catch (const std::runtime_error &e) {
    std::cerr << e.what() << std::endl;
    std::cerr << parser;
    return EXIT_FAILURE;
}

and the user messes up by misformatting the "number" positional argument, this will throw an std::invalid_argument from argparse::details::do_from_chars. However, std::invalid_argument inherits from std::logic_error, not std::runtime_error, so the user will just have an aborted program with no help message. In order to keep the code simple while not having to catch std::runtime_error and std::logic_error separately, the user could use std::exception instead to catch all errors of parse_args.

…td::runtime_error

parse_args can throw exceptions that are not based on std::runtime_error, and the
error message should show on all errors.
@p-ranav p-ranav merged commit 9fe48c7 into p-ranav:master Oct 26, 2023
8 checks passed
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

Successfully merging this pull request may close these issues.

2 participants