diff --git a/README.md b/README.md index ff9a9a956..0318c7c6d 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ app.add_option("-f,--file", filename, "A help string"); try { app.parse(argc, argv); -} catch (const CLI::Error &e) { +} catch (const CLI::ParseError &e) { return app.exit(e); } ``` @@ -137,7 +137,7 @@ App* subcom = app.add_subcommand(name, discription); An option name must start with a alphabetic character or underscore. For long options, anything but an equals sign or a comma is valid after that. Names are given as a comma separated string, with the dash or dashes. An option or flag can have as many names as you want, and afterward, using `count`, you can use any of the names, with dashes as needed, to count the options. One of the names is allowed to be given without proceeding dash(es); if present the option is a positional option, and that name will be used on help line for its positional form. If you want the default value to print in the help description, pass in `true` for the final parameter for `add_option` or `add_set`. The set options allow your users to pick from a set of predefined options. -On a C++14 compiler, you can pass a callback function directly to `.add_flag`, while in C++11 mode you'll need to use `.add_flag_function` if you want a callback function. The function will be given the number of times the flag was passed. You can throw a relevant `CLI::Error` to signal a failure. +On a C++14 compiler, you can pass a callback function directly to `.add_flag`, while in C++11 mode you'll need to use `.add_flag_function` if you want a callback function. The function will be given the number of times the flag was passed. You can throw a relevant `CLI::ParseError` to signal a failure. ### Example @@ -288,7 +288,7 @@ If you use the excellent [Rang] library to add color to your terminal in a safe, std::atexit([](){std::cout << rang::style::reset;}); try { app.parse(argc, argv); -} catch (const CLI::Error &e) { +} catch (const CLI::ParseError &e) { std::cout << (e.get_exit_code()==0 ? rang::fg::blue : rang::fg::red); return app.exit(e); } diff --git a/examples/enum.cpp b/examples/enum.cpp index c03799cf1..827bee392 100644 --- a/examples/enum.cpp +++ b/examples/enum.cpp @@ -9,10 +9,7 @@ int main(int argc, char **argv) { app.add_set("-l,--level", level, {High, Medium, Low}, "Level settings") ->set_type_name("enum/Level in {High=0, Medium=1, Low=2}"); - try { - app.parse(argc, argv); - } catch(CLI::Error const &e) { - app.exit(e); - } + CLI11_PARSE(app, argc, argv); + return 0; } diff --git a/examples/groups.cpp b/examples/groups.cpp index 0b6b109b8..f8de448fd 100644 --- a/examples/groups.cpp +++ b/examples/groups.cpp @@ -17,7 +17,7 @@ int main(int argc, char **argv) { try { app.parse(argc, argv); - } catch(const CLI::Error &e) { + } catch(const CLI::ParseError &e) { return app.exit(e); } diff --git a/examples/inter_argument_order.cpp b/examples/inter_argument_order.cpp index 6192c3bce..6609fd8be 100644 --- a/examples/inter_argument_order.cpp +++ b/examples/inter_argument_order.cpp @@ -15,10 +15,10 @@ int main(int argc, char **argv) { app.add_flag("--z,--x"); // Random other flags - // Standard parsing lines (copy and paste in) + // Standard parsing lines (copy and paste in, or use CLI11_PARSE) try { app.parse(argc, argv); - } catch(const CLI::Error &e) { + } catch(const CLI::ParseError &e) { return app.exit(e); } diff --git a/examples/subcommands.cpp b/examples/subcommands.cpp index 39bc7eb62..a28b2aac2 100644 --- a/examples/subcommands.cpp +++ b/examples/subcommands.cpp @@ -12,11 +12,7 @@ int main(int argc, char **argv) { CLI::Option *s = stop->add_flag("-c,--count", "Counter"); - try { - app.parse(argc, argv); - } catch(const CLI::Error &e) { - return app.exit(e); - } + CLI11_PARSE(app, argc, argv); std::cout << "Working on file: " << file << ", direct count: " << start->count("--file") << std::endl; std::cout << "Working on count: " << s->count() << ", direct count: " << stop->count("--count") << std::endl; diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index b3d112093..dd04492f6 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -29,7 +29,7 @@ namespace CLI { #define CLI11_PARSE(app, argc, argv) \ try { \ (app).parse((argc), (argv)); \ - } catch(const CLI::Error &e) { \ + } catch(const CLI::ParseError &e) { \ return (app).exit(e); \ } #endif diff --git a/include/CLI/Error.hpp b/include/CLI/Error.hpp index 25045a652..4631b714e 100644 --- a/include/CLI/Error.hpp +++ b/include/CLI/Error.hpp @@ -79,19 +79,17 @@ struct OptionAlreadyAdded : public ConstructionError { : ConstructionError("OptionAlreadyAdded", name, ExitCodes::OptionAlreadyAdded) {} }; -// Runtime Errors - -/// Does not output a diagnostic in CLI11_PARSE, but allows to return from main() with a specific error code. -struct RuntimeError : public Error { - RuntimeError(int exit_code = 1) : Error("RuntimeError", "runtime error", exit_code, false) {} -}; - // Parsing errors /// Anything that can error in Parse struct ParseError : public Error { ParseError(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true) : Error(parent, name, exit_code, print_help) {} + ParseError(std::string parent, + std::string name, + int exit_code = static_cast(ExitCodes::BaseClass), + bool print_help = true) + : Error(parent, name, exit_code, print_help) {} }; // Not really "errors" @@ -107,6 +105,11 @@ struct CallForHelp : public ParseError { : ParseError("CallForHelp", "This should be caught in your main function, see examples", ExitCodes::Success) {} }; +/// Does not output a diagnostic in CLI11_PARSE, but allows to return from main() with a specific error code. +struct RuntimeError : public ParseError { + RuntimeError(int exit_code = 1) : ParseError("RuntimeError", "runtime error", exit_code, false) {} +}; + /// Thrown when parsing an INI file and it is missing struct FileError : public ParseError { FileError(std::string name) : ParseError("FileError", name, ExitCodes::File) {}