Skip to content

Commit

Permalink
Making RuntimeError a ParseError
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Nov 20, 2017
1 parent 7b433ec commit d3c684a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 21 deletions.
7 changes: 2 additions & 5 deletions examples/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion examples/groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/inter_argument_order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 1 addition & 5 deletions examples/subcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions include/CLI/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ 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
Expand All @@ -107,6 +100,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) : Error("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) {}
Expand Down

0 comments on commit d3c684a

Please sign in to comment.