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 539d965
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
Expand Down
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
17 changes: 10 additions & 7 deletions include/CLI/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(ExitCodes::BaseClass),
bool print_help = true)
: Error(parent, name, exit_code, print_help) {}
};

// Not really "errors"
Expand All @@ -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) {}
Expand Down

0 comments on commit 539d965

Please sign in to comment.