26#define CLI11_ERROR_DEF(parent, name) \
28 name(std::string ename, std::string msg, int exit_code) : parent(std::move(ename), std::move(msg), exit_code) {} \
29 name(std::string ename, std::string msg, ExitCodes exit_code) \
30 : parent(std::move(ename), std::move(msg), exit_code) {} \
33 name(std::string msg, ExitCodes exit_code) : parent(#name, std::move(msg), exit_code) {} \
34 name(std::string msg, int exit_code) : parent(#name, std::move(msg), exit_code) {}
37#define CLI11_ERROR_SIMPLE(name) \
38 explicit name(std::string msg) : name(#name, msg, ExitCodes::name) {}
71class Error :
public std::runtime_error {
73 std::string error_name{
"Error"};
81 : runtime_error(msg), actual_exit_code(exit_code), error_name(std::move(name)) {}
83 Error(std::string name, std::string msg,
ExitCodes exit_code) :
Error(name, msg, static_cast<int>(exit_code)) {}
111 name +
": You can't change expected arguments after you've changed the multi option policy!");
117 return IncorrectConstruction(name +
": multi_option_policy only works for flags and exact value options");
128 return BadNameString(
"Must have a name, not just dashes: " + name);
130 static BadNameString MultiPositionalNames(std::string name) {
131 return BadNameString(
"Only one positional name allowed, remove: " + name);
193 static FileError Missing(std::string name) {
return FileError(name +
" was not readable (missing?)"); }
201 :
ConversionError(
"The value " + member +
" is not an allowed value for " + name) {}
224 if(min_subcom == 1) {
230 Option(std::size_t min_option, std::size_t max_option, std::size_t used,
const std::string &option_list) {
231 if((min_option == 1) && (max_option == 1) && (used == 0))
232 return RequiredError(
"Exactly 1 option from [" + option_list +
"]");
233 if((min_option == 1) && (max_option == 1) && (used > 1)) {
234 return {
"Exactly 1 option from [" + option_list +
"] is required and " + std::to_string(used) +
238 if((min_option == 1) && (used == 0))
239 return RequiredError(
"At least 1 option from [" + option_list +
"]");
240 if(used < min_option) {
241 return {
"Requires at least " + std::to_string(min_option) +
" options used and only " +
242 std::to_string(used) +
"were given from [" + option_list +
"]",
248 return {
"Requires at most " + std::to_string(max_option) +
" options be used and " + std::to_string(used) +
249 "were given from [" + option_list +
"]",
259 :
ArgumentMismatch(expected > 0 ? (
"Expected exactly " + std::to_string(expected) +
" arguments to " + name +
260 ", got " + std::to_string(received))
261 : (
"Expected at least " + std::to_string(-expected) +
" arguments to " + name +
262 ", got " + std::to_string(received)),
265 static ArgumentMismatch AtLeast(std::string name,
int num, std::size_t received) {
266 return ArgumentMismatch(name +
": At least " + std::to_string(num) +
" required but received " +
267 std::to_string(received));
269 static ArgumentMismatch AtMost(std::string name,
int num, std::size_t received) {
270 return ArgumentMismatch(name +
": At Most " + std::to_string(num) +
" required but received " +
271 std::to_string(received));
273 static ArgumentMismatch TypedAtLeast(std::string name,
int num, std::string type) {
274 return ArgumentMismatch(name +
": " + std::to_string(num) +
" required " + type +
" missing");
279 static ArgumentMismatch PartialType(std::string name,
int num, std::string type) {
280 return ArgumentMismatch(name +
": " + type +
" only partially specified: " + std::to_string(num) +
281 " required for each element");
302 explicit ExtrasError(std::vector<std::string> args)
303 :
ExtrasError((args.size() > 1 ?
"The following arguments were not expected: "
304 :
"The following argument was not expected: ") +
307 ExtrasError(
const std::string &name, std::vector<std::string> args)
309 (args.size() > 1 ?
"The following arguments were not expected: "
310 :
"The following argument was not expected: ") +
320 static ConfigError NotConfigurable(std::string item) {
321 return ConfigError(item +
": This option is not allowed in a configuration file");
348#undef CLI11_ERROR_DEF
349#undef CLI11_ERROR_SIMPLE
#define CLI11_ERROR_SIMPLE(name)
Definition: Error.hpp:37
#define CLI11_ERROR_DEF(parent, name)
Definition: Error.hpp:26
#define CLI11_NODISCARD
Definition: Macros.hpp:47
Thrown when the wrong number of arguments has been received.
Definition: Error.hpp:255
Thrown on construction of a bad name.
Definition: Error.hpp:122
Usually something like –help-all on command line.
Definition: Error.hpp:170
-h or –help on command line
Definition: Error.hpp:164
-v or –version on command line
Definition: Error.hpp:177
Thrown when extra values are found in an INI file.
Definition: Error.hpp:316
Construction errors (not in parsing)
Definition: Error.hpp:89
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition: Error.hpp:197
All errors derive from this one.
Definition: Error.hpp:71
CLI11_NODISCARD int get_exit_code() const
Definition: Error.hpp:76
Error(std::string name, std::string msg, ExitCodes exit_code)
Definition: Error.hpp:83
CLI11_NODISCARD std::string get_name() const
Definition: Error.hpp:78
Error(std::string name, std::string msg, int exit_code=static_cast< int >(ExitCodes::BaseClass))
Definition: Error.hpp:80
Thrown when an excludes option is present.
Definition: Error.hpp:293
Thrown when parsing an INI file and it is missing.
Definition: Error.hpp:190
Definition: Error.hpp:335
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition: Error.hpp:94
Thrown when validation fails before parsing.
Definition: Error.hpp:326
Thrown when an option already exists.
Definition: Error.hpp:136
Thrown when counting a non-existent option.
Definition: Error.hpp:343
Definition: Option.hpp:228
Anything that can error in Parse.
Definition: Error.hpp:151
Thrown when a required option is missing.
Definition: Error.hpp:220
Thrown when a requires option is missing.
Definition: Error.hpp:286
Does not output a diagnostic in CLI11_PARSE, but allows main() to return with a specific error code.
Definition: Error.hpp:184
This is a successful completion on parsing, supposed to exit.
Definition: Error.hpp:158
Thrown when validation of results fails.
Definition: Error.hpp:213
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:51
std::string rjoin(const T &v, std::string delim=",")
Join a string in reverse order.
Definition: StringTools.hpp:84
ExitCodes
Definition: Error.hpp:42
MultiOptionPolicy
Enumeration of the multiOption Policy selection.
Definition: Option.hpp:38