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) {}
73class 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(
"Must have a name, not just dashes: " + name);
138 static BadNameString MultiPositionalNames(std::string name) {
139 return BadNameString(
"Only one positional name allowed, remove: " + name);
148 static OptionAlreadyAdded Requires(std::string name, std::string other) {
151 static OptionAlreadyAdded Excludes(std::string name, std::string other) {
172class CallForHelp :
public Success {
174 CallForHelp() : CallForHelp(
"This should be caught in your main function, see examples",
ExitCodes::Success) {}
178class CallForAllHelp :
public Success {
181 : CallForAllHelp(
"This should be caught in your main function, see examples",
ExitCodes::Success) {}
185class CallForVersion :
public Success {
188 : CallForVersion(
"This should be caught in your main function, see examples",
ExitCodes::Success) {}
194 explicit RuntimeError(
int exit_code = 1) : RuntimeError(
"Runtime error", exit_code) {}
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) {}
212 static ConversionError TooManyInputsFlag(std::string name) {
215 static ConversionError TrueFalse(std::string name) {
231 static RequiredError Subcommand(std::size_t min_subcom) {
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 " +
250 std::to_string(used) +
" were given from [" + option_list +
"]",
256 return {
"Requires at most " + std::to_string(max_option) +
" options be used but " + std::to_string(used) +
257 " were given from [" + option_list +
"]",
267 :
ArgumentMismatch(expected > 0 ? (
"Expected exactly " + std::to_string(expected) +
" arguments to " + name +
268 ", got " + std::to_string(received))
269 : (
"Expected at least " + std::to_string(-expected) +
" arguments to " + name +
270 ", got " + std::to_string(received)),
273 static ArgumentMismatch AtLeast(std::string name,
int num, std::size_t received) {
274 return ArgumentMismatch(name +
": At least " + std::to_string(num) +
" required but received " +
275 std::to_string(received));
277 static ArgumentMismatch AtMost(std::string name,
int num, std::size_t received) {
278 return ArgumentMismatch(name +
": At Most " + std::to_string(num) +
" required but received " +
279 std::to_string(received));
281 static ArgumentMismatch TypedAtLeast(std::string name,
int num, std::string type) {
282 return ArgumentMismatch(name +
": " + std::to_string(num) +
" required " + type +
" missing");
284 static ArgumentMismatch FlagOverride(std::string name) {
287 static ArgumentMismatch PartialType(std::string name,
int num, std::string type) {
288 return ArgumentMismatch(name +
": " + type +
" only partially specified: " + std::to_string(num) +
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");
351class OptionNotFound :
public Error {
356#undef CLI11_ERROR_DEF
357#undef CLI11_ERROR_SIMPLE
#define CLI11_ERROR_SIMPLE(name)
Definition Error.hpp:39
#define CLI11_ERROR_DEF(parent, name)
Definition Error.hpp:28
#define CLI11_NODISCARD
Definition Macros.hpp:49
Thrown when the wrong number of arguments has been received.
Definition Error.hpp:263
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
Construction errors (not in parsing)
Definition Error.hpp:91
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition Error.hpp:205
CLI11_NODISCARD int get_exit_code() const
Definition Error.hpp:78
Error(std::string name, std::string msg, ExitCodes exit_code)
Definition Error.hpp:85
CLI11_NODISCARD std::string get_name() const
Definition Error.hpp:80
Error(std::string name, std::string msg, int exit_code=static_cast< int >(ExitCodes::BaseClass))
Definition Error.hpp:82
Thrown when an excludes option is present.
Definition Error.hpp:301
Thrown when parsing an INI file and it is missing.
Definition Error.hpp:198
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition Error.hpp:96
Thrown when validation fails before parsing.
Definition Error.hpp:334
Thrown when an option already exists.
Definition Error.hpp:144
Thrown when counting a non-existent option.
Definition Error.hpp:351
Anything that can error in Parse.
Definition Error.hpp:159
Thrown when a required option is missing.
Definition Error.hpp:228
Thrown when a requires option is missing.
Definition Error.hpp:294
This is a successful completion on parsing, supposed to exit.
Definition Error.hpp:166
Thrown when validation of results fails.
Definition Error.hpp:221
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition StringTools.hpp:53
std::string rjoin(const T &v, std::string delim=",")
Join a string in reverse order.
Definition StringTools.hpp:86
ExitCodes
Definition Error.hpp:44
@ FileError
Definition Error.hpp:49
@ ConfigError
Definition Error.hpp:56
@ ExtrasError
Definition Error.hpp:55
@ ExcludesError
Definition Error.hpp:54
@ ConversionError
Definition Error.hpp:50
@ ArgumentMismatch
Definition Error.hpp:60
@ BaseClass
Definition Error.hpp:61
@ Success
Definition Error.hpp:45
@ RequiresError
Definition Error.hpp:53
@ ValidationError
Definition Error.hpp:51
@ IncorrectConstruction
Definition Error.hpp:46
@ RequiredError
Definition Error.hpp:52
@ OptionNotFound
Definition Error.hpp:59
@ BadNameString
Definition Error.hpp:47
@ OptionAlreadyAdded
Definition Error.hpp:48
@ InvalidError
Definition Error.hpp:57
MultiOptionPolicy
Enumeration of the multiOption Policy selection.
Definition Option.hpp:40