28 #define CLI11_ERROR_DEF(parent, name) \
30 name(std::string ename, std::string msg, int exit_code) : parent(std::move(ename), std::move(msg), exit_code) {} \
31 name(std::string ename, std::string msg, ExitCodes exit_code) \
32 : parent(std::move(ename), std::move(msg), exit_code) {} \
35 name(std::string msg, ExitCodes exit_code) : parent(#name, std::move(msg), exit_code) {} \
36 name(std::string msg, int exit_code) : parent(#name, std::move(msg), exit_code) {}
39 #define CLI11_ERROR_SIMPLE(name) \
40 explicit name(std::string msg) : name(#name, msg, ExitCodes::name) {}
73 class Error :
public std::runtime_error {
75 std::string error_name{
"Error"};
83 : runtime_error(msg), actual_exit_code(exit_code), error_name(
std::move(name)) {}
85 Error(std::string name, std::string msg,
ExitCodes exit_code) :
Error(name, msg, static_cast<int>(exit_code)) {}
113 name +
": You can't change expected arguments after you've changed the multi option policy!");
119 return IncorrectConstruction(name +
": multi_option_policy only works for flags and exact value options");
129 return BadNameString(
"Long names strings require 2 dashes " + name);
136 return BadNameString(
"Names '-','--','++' are reserved and not allowed as option names " + name);
138 static BadNameString MultiPositionalNames(std::string name) {
139 return BadNameString(
"Only one positional name allowed, remove: " + name);
201 static FileError Missing(std::string name) {
return FileError(name +
" was not readable (missing?)"); }
209 :
ConversionError(
"The value " + member +
" is not an allowed value for " + name) {}
232 if(min_subcom == 1) {
238 Option(std::size_t min_option, std::size_t max_option, std::size_t used,
const std::string &option_list) {
239 if((min_option == 1) && (max_option == 1) && (used == 0))
240 return RequiredError(
"Exactly 1 option from [" + option_list +
"]");
241 if((min_option == 1) && (max_option == 1) && (used > 1)) {
242 return {
"Exactly 1 option from [" + option_list +
"] is required but " +
std::to_string(used) +
246 if((min_option == 1) && (used == 0))
247 return RequiredError(
"At least 1 option from [" + option_list +
"]");
248 if(used < min_option) {
249 return {
"Requires at least " +
std::to_string(min_option) +
" options used but only " +
257 " were given from [" + option_list +
"]",
269 : (
"Expected at least " +
std::to_string(-expected) +
" arguments to " + name +
273 static ArgumentMismatch AtLeast(std::string name,
int num, std::size_t received) {
277 static ArgumentMismatch AtMost(std::string name,
int num, std::size_t received) {
281 static ArgumentMismatch TypedAtLeast(std::string name,
int num, std::string type) {
287 static ArgumentMismatch PartialType(std::string name,
int num, std::string type) {
289 " required for each element");
310 explicit ExtrasError(std::vector<std::string> args)
311 :
ExtrasError((args.size() > 1 ?
"The following arguments were not expected: "
312 :
"The following argument was not expected: ") +
315 ExtrasError(
const std::string &name, std::vector<std::string> args)
317 (args.size() > 1 ?
"The following arguments were not expected: "
318 :
"The following argument was not expected: ") +
328 static ConfigError NotConfigurable(std::string item) {
329 return ConfigError(item +
": This option is not allowed in a configuration file");
356 #undef CLI11_ERROR_DEF
357 #undef CLI11_ERROR_SIMPLE
Error(std::string name, std::string msg, ExitCodes exit_code)
Definition: Error.hpp:85
Thrown when an option already exists.
Definition: Error.hpp:144
Definition: Error.hpp:343
Anything that can error in Parse.
Definition: Error.hpp:159
Does not output a diagnostic in CLI11_PARSE, but allows main() to return with a specific error code...
Definition: Error.hpp:192
ExitCodes
Definition: Error.hpp:44
Error(std::string name, std::string msg, int exit_code=static_cast< int >(ExitCodes::BaseClass))
Definition: Error.hpp:82
Definition: Option.hpp:231
#define CLI11_ERROR_DEF(parent, name)
Definition: Error.hpp:28
std::string rjoin(const T &v, std::string delim=",")
Join a string in reverse order.
Definition: StringTools.hpp:91
MultiOptionPolicy
Enumeration of the multiOption Policy selection.
Definition: Option.hpp:40
CLI11_NODISCARD int get_exit_code() const
Definition: Error.hpp:78
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:337
Thrown when a required option is missing.
Definition: Error.hpp:228
Thrown when parsing an INI file and it is missing.
Definition: Error.hpp:198
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:53
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition: Error.hpp:205
Usually something like –help-all on command line.
Definition: Error.hpp:178
Thrown when a requires option is missing.
Definition: Error.hpp:294
Construction errors (not in parsing)
Definition: Error.hpp:91
All errors derive from this one.
Definition: Error.hpp:73
Thrown when validation of results fails.
Definition: Error.hpp:221
Thrown when the wrong number of arguments has been received.
Definition: Error.hpp:263
CLI11_NODISCARD std::string get_name() const
Definition: Error.hpp:80
#define CLI11_NODISCARD
Definition: Macros.hpp:58
This is a successful completion on parsing, supposed to exit.
Definition: Error.hpp:166
#define CLI11_ERROR_SIMPLE(name)
Definition: Error.hpp:39
Thrown when an option is set to conflicting values (non-vector and multi args, for example) ...
Definition: Error.hpp:96
Thrown when an excludes option is present.
Definition: Error.hpp:301
-v or –version on command line
Definition: Error.hpp:185
Thrown when validation fails before parsing.
Definition: Error.hpp:334
Thrown on construction of a bad name.
Definition: Error.hpp:124
Thrown when extra values are found in an INI file.
Definition: Error.hpp:324
-h or –help on command line
Definition: Error.hpp:172
Thrown when counting a nonexistent option.
Definition: Error.hpp:351