mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 04:33:53 +00:00
Making RuntimeError a ParseError
This commit is contained in:
parent
0bca8fdd30
commit
0395467d48
@ -96,7 +96,7 @@ app.add_option("-f,--file", filename, "A help string");
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
app.parse(argc, argv);
|
app.parse(argc, argv);
|
||||||
} catch (const CLI::Error &e) {
|
} catch (const CLI::ParseError &e) {
|
||||||
return app.exit(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.
|
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
|
### Example
|
||||||
|
|
||||||
@ -289,7 +289,7 @@ If you use the excellent [Rang] library to add color to your terminal in a safe,
|
|||||||
std::atexit([](){std::cout << rang::style::reset;});
|
std::atexit([](){std::cout << rang::style::reset;});
|
||||||
try {
|
try {
|
||||||
app.parse(argc, argv);
|
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);
|
std::cout << (e.get_exit_code()==0 ? rang::fg::blue : rang::fg::red);
|
||||||
return app.exit(e);
|
return app.exit(e);
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,7 @@ int main(int argc, char **argv) {
|
|||||||
app.add_set("-l,--level", level, {High, Medium, Low}, "Level settings")
|
app.add_set("-l,--level", level, {High, Medium, Low}, "Level settings")
|
||||||
->set_type_name("enum/Level in {High=0, Medium=1, Low=2}");
|
->set_type_name("enum/Level in {High=0, Medium=1, Low=2}");
|
||||||
|
|
||||||
try {
|
CLI11_PARSE(app, argc, argv);
|
||||||
app.parse(argc, argv);
|
|
||||||
} catch(CLI::Error const &e) {
|
|
||||||
app.exit(e);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
app.parse(argc, argv);
|
app.parse(argc, argv);
|
||||||
} catch(const CLI::Error &e) {
|
} catch(const CLI::ParseError &e) {
|
||||||
return app.exit(e);
|
return app.exit(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,10 +15,10 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
app.add_flag("--z,--x"); // Random other flags
|
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 {
|
try {
|
||||||
app.parse(argc, argv);
|
app.parse(argc, argv);
|
||||||
} catch(const CLI::Error &e) {
|
} catch(const CLI::ParseError &e) {
|
||||||
return app.exit(e);
|
return app.exit(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,11 +12,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
CLI::Option *s = stop->add_flag("-c,--count", "Counter");
|
CLI::Option *s = stop->add_flag("-c,--count", "Counter");
|
||||||
|
|
||||||
try {
|
CLI11_PARSE(app, argc, argv);
|
||||||
app.parse(argc, argv);
|
|
||||||
} catch(const CLI::Error &e) {
|
|
||||||
return app.exit(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "Working on file: " << file << ", direct count: " << start->count("--file") << std::endl;
|
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;
|
std::cout << "Working on count: " << s->count() << ", direct count: " << stop->count("--count") << std::endl;
|
||||||
|
@ -29,7 +29,7 @@ namespace CLI {
|
|||||||
#define CLI11_PARSE(app, argc, argv) \
|
#define CLI11_PARSE(app, argc, argv) \
|
||||||
try { \
|
try { \
|
||||||
(app).parse((argc), (argv)); \
|
(app).parse((argc), (argv)); \
|
||||||
} catch(const CLI::Error &e) { \
|
} catch(const CLI::ParseError &e) { \
|
||||||
return (app).exit(e); \
|
return (app).exit(e); \
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -79,19 +79,17 @@ struct OptionAlreadyAdded : public ConstructionError {
|
|||||||
: ConstructionError("OptionAlreadyAdded", name, ExitCodes::OptionAlreadyAdded) {}
|
: 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
|
// Parsing errors
|
||||||
|
|
||||||
/// Anything that can error in Parse
|
/// Anything that can error in Parse
|
||||||
struct ParseError : public Error {
|
struct ParseError : public Error {
|
||||||
ParseError(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true)
|
ParseError(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true)
|
||||||
: Error(parent, name, exit_code, print_help) {}
|
: 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"
|
// 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) {}
|
: 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
|
/// Thrown when parsing an INI file and it is missing
|
||||||
struct FileError : public ParseError {
|
struct FileError : public ParseError {
|
||||||
FileError(std::string name) : ParseError("FileError", name, ExitCodes::File) {}
|
FileError(std::string name) : ParseError("FileError", name, ExitCodes::File) {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user