Commit 0395467d488ce4a075499763301fa6b303c89a67
Committed by
Henry Schreiner
1 parent
0bca8fdd
Making RuntimeError a ParseError
Showing
7 changed files
with
20 additions
and
24 deletions
README.md
| ... | ... | @@ -96,7 +96,7 @@ app.add_option("-f,--file", filename, "A help string"); |
| 96 | 96 | |
| 97 | 97 | try { |
| 98 | 98 | app.parse(argc, argv); |
| 99 | -} catch (const CLI::Error &e) { | |
| 99 | +} catch (const CLI::ParseError &e) { | |
| 100 | 100 | return app.exit(e); |
| 101 | 101 | } |
| 102 | 102 | ``` |
| ... | ... | @@ -137,7 +137,7 @@ App* subcom = app.add_subcommand(name, discription); |
| 137 | 137 | |
| 138 | 138 | 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. |
| 139 | 139 | |
| 140 | -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. | |
| 140 | +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. | |
| 141 | 141 | |
| 142 | 142 | ### Example |
| 143 | 143 | |
| ... | ... | @@ -289,7 +289,7 @@ If you use the excellent [Rang] library to add color to your terminal in a safe, |
| 289 | 289 | std::atexit([](){std::cout << rang::style::reset;}); |
| 290 | 290 | try { |
| 291 | 291 | app.parse(argc, argv); |
| 292 | -} catch (const CLI::Error &e) { | |
| 292 | +} catch (const CLI::ParseError &e) { | |
| 293 | 293 | std::cout << (e.get_exit_code()==0 ? rang::fg::blue : rang::fg::red); |
| 294 | 294 | return app.exit(e); |
| 295 | 295 | } | ... | ... |
examples/enum.cpp
| ... | ... | @@ -9,10 +9,7 @@ int main(int argc, char **argv) { |
| 9 | 9 | app.add_set("-l,--level", level, {High, Medium, Low}, "Level settings") |
| 10 | 10 | ->set_type_name("enum/Level in {High=0, Medium=1, Low=2}"); |
| 11 | 11 | |
| 12 | - try { | |
| 13 | - app.parse(argc, argv); | |
| 14 | - } catch(CLI::Error const &e) { | |
| 15 | - app.exit(e); | |
| 16 | - } | |
| 12 | + CLI11_PARSE(app, argc, argv); | |
| 13 | + | |
| 17 | 14 | return 0; |
| 18 | 15 | } | ... | ... |
examples/groups.cpp
examples/inter_argument_order.cpp
| ... | ... | @@ -15,10 +15,10 @@ int main(int argc, char **argv) { |
| 15 | 15 | |
| 16 | 16 | app.add_flag("--z,--x"); // Random other flags |
| 17 | 17 | |
| 18 | - // Standard parsing lines (copy and paste in) | |
| 18 | + // Standard parsing lines (copy and paste in, or use CLI11_PARSE) | |
| 19 | 19 | try { |
| 20 | 20 | app.parse(argc, argv); |
| 21 | - } catch(const CLI::Error &e) { | |
| 21 | + } catch(const CLI::ParseError &e) { | |
| 22 | 22 | return app.exit(e); |
| 23 | 23 | } |
| 24 | 24 | ... | ... |
examples/subcommands.cpp
| ... | ... | @@ -12,11 +12,7 @@ int main(int argc, char **argv) { |
| 12 | 12 | |
| 13 | 13 | CLI::Option *s = stop->add_flag("-c,--count", "Counter"); |
| 14 | 14 | |
| 15 | - try { | |
| 16 | - app.parse(argc, argv); | |
| 17 | - } catch(const CLI::Error &e) { | |
| 18 | - return app.exit(e); | |
| 19 | - } | |
| 15 | + CLI11_PARSE(app, argc, argv); | |
| 20 | 16 | |
| 21 | 17 | std::cout << "Working on file: " << file << ", direct count: " << start->count("--file") << std::endl; |
| 22 | 18 | std::cout << "Working on count: " << s->count() << ", direct count: " << stop->count("--count") << std::endl; | ... | ... |
include/CLI/App.hpp
include/CLI/Error.hpp
| ... | ... | @@ -79,19 +79,17 @@ struct OptionAlreadyAdded : public ConstructionError { |
| 79 | 79 | : ConstructionError("OptionAlreadyAdded", name, ExitCodes::OptionAlreadyAdded) {} |
| 80 | 80 | }; |
| 81 | 81 | |
| 82 | -// Runtime Errors | |
| 83 | - | |
| 84 | -/// Does not output a diagnostic in CLI11_PARSE, but allows to return from main() with a specific error code. | |
| 85 | -struct RuntimeError : public Error { | |
| 86 | - RuntimeError(int exit_code = 1) : Error("RuntimeError", "runtime error", exit_code, false) {} | |
| 87 | -}; | |
| 88 | - | |
| 89 | 82 | // Parsing errors |
| 90 | 83 | |
| 91 | 84 | /// Anything that can error in Parse |
| 92 | 85 | struct ParseError : public Error { |
| 93 | 86 | ParseError(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true) |
| 94 | 87 | : Error(parent, name, exit_code, print_help) {} |
| 88 | + ParseError(std::string parent, | |
| 89 | + std::string name, | |
| 90 | + int exit_code = static_cast<int>(ExitCodes::BaseClass), | |
| 91 | + bool print_help = true) | |
| 92 | + : Error(parent, name, exit_code, print_help) {} | |
| 95 | 93 | }; |
| 96 | 94 | |
| 97 | 95 | // Not really "errors" |
| ... | ... | @@ -107,6 +105,11 @@ struct CallForHelp : public ParseError { |
| 107 | 105 | : ParseError("CallForHelp", "This should be caught in your main function, see examples", ExitCodes::Success) {} |
| 108 | 106 | }; |
| 109 | 107 | |
| 108 | +/// Does not output a diagnostic in CLI11_PARSE, but allows to return from main() with a specific error code. | |
| 109 | +struct RuntimeError : public ParseError { | |
| 110 | + RuntimeError(int exit_code = 1) : ParseError("RuntimeError", "runtime error", exit_code, false) {} | |
| 111 | +}; | |
| 112 | + | |
| 110 | 113 | /// Thrown when parsing an INI file and it is missing |
| 111 | 114 | struct FileError : public ParseError { |
| 112 | 115 | FileError(std::string name) : ParseError("FileError", name, ExitCodes::File) {} | ... | ... |