12 #include "CLI/StringTools.hpp" 18 #define CLI11_ERROR_DEF(parent, name) \ 20 name(std::string ename, std::string msg, int exit_code) : parent(std::move(ename), std::move(msg), exit_code) {} \ 21 name(std::string ename, std::string msg, ExitCodes exit_code) \ 22 : parent(std::move(ename), std::move(msg), exit_code) {} \ 25 name(std::string msg, ExitCodes exit_code) : parent(#name, std::move(msg), exit_code) {} \ 26 name(std::string msg, int exit_code) : parent(#name, std::move(msg), exit_code) {} 29 #define CLI11_ERROR_SIMPLE(name) \ 30 explicit name(std::string msg) : name(#name, msg, ExitCodes::name) {} 34 enum class ExitCodes {
36 IncorrectConstruction = 100,
63 class Error :
public std::runtime_error {
65 std::string error_name{
"Error"};
68 int get_exit_code()
const {
return actual_exit_code; }
70 std::string get_name()
const {
return error_name; }
72 Error(std::string name, std::string msg,
int exit_code = static_cast<int>(ExitCodes::BaseClass))
73 : runtime_error(msg), actual_exit_code(exit_code), error_name(std::move(name)) {}
75 Error(std::string name, std::string msg, ExitCodes exit_code) :
Error(name, msg, static_cast<int>(exit_code)) {}
103 name +
": You can't change expected arguments after you've changed the multi option policy!");
109 return IncorrectConstruction(name +
": multi_option_policy only works for flags and exact value options");
120 return BadNameString(
"Must have a name, not just dashes: " + name);
122 static BadNameString MultiPositionalNames(std::string name) {
123 return BadNameString(
"Only one positional name allowed, remove: " + name);
133 return OptionAlreadyAdded(name +
" requires " + other, ExitCodes::OptionAlreadyAdded);
136 return OptionAlreadyAdded(name +
" excludes " + other, ExitCodes::OptionAlreadyAdded);
152 Success() :
Success(
"Successfully completed, should be caught and quit", ExitCodes::Success) {}
158 CallForHelp() :
CallForHelp(
"This should be caught in your main function, see examples", ExitCodes::Success) {}
165 :
CallForAllHelp(
"This should be caught in your main function, see examples", ExitCodes::Success) {}
178 static FileError Missing(std::string name) {
return FileError(name +
" was not readable (missing?)"); }
186 :
ConversionError(
"The value " + member +
" is not an allowed value for " + name) {}
188 :
ConversionError(
"Could not convert: " + name +
" = " + detail::join(results)) {}
212 return RequiredError(
"Requires at least " + std::to_string(min_subcom) +
" subcommands",
213 ExitCodes::RequiredError);
215 static RequiredError Option(
size_t min_option,
size_t max_option,
size_t used,
const std::string &option_list) {
216 if((min_option == 1) && (max_option == 1) && (used == 0))
217 return RequiredError(
"Exactly 1 option from [" + option_list +
"]");
218 else if((min_option == 1) && (max_option == 1) && (used > 1))
219 return RequiredError(
"Exactly 1 option from [" + option_list +
"] is required and " + std::to_string(used) +
221 ExitCodes::RequiredError);
222 else if((min_option == 1) && (used == 0))
223 return RequiredError(
"At least 1 option from [" + option_list +
"]");
224 else if(used < min_option)
225 return RequiredError(
"Requires at least " + std::to_string(min_option) +
" options used and only " +
226 std::to_string(used) +
"were given from [" + option_list +
"]",
227 ExitCodes::RequiredError);
228 else if(max_option == 1)
229 return RequiredError(
"Requires at most 1 options be given from [" + option_list +
"]",
230 ExitCodes::RequiredError);
232 return RequiredError(
"Requires at most " + std::to_string(max_option) +
" options be used and " +
233 std::to_string(used) +
"were given from [" + option_list +
"]",
234 ExitCodes::RequiredError);
243 :
ArgumentMismatch(expected > 0 ? (
"Expected exactly " + std::to_string(expected) +
" arguments to " + name +
244 ", got " + std::to_string(recieved))
245 : (
"Expected at least " + std::to_string(-expected) +
" arguments to " + name +
246 ", got " + std::to_string(recieved)),
247 ExitCodes::ArgumentMismatch) {}
250 return ArgumentMismatch(name +
": At least " + std::to_string(num) +
" required");
252 static ArgumentMismatch TypedAtLeast(std::string name,
int num, std::string type) {
253 return ArgumentMismatch(name +
": " + std::to_string(num) +
" required " + type +
" missing");
264 :
RequiresError(curname +
" requires " + subname, ExitCodes::RequiresError) {}
271 :
ExcludesError(curname +
" excludes " + subname, ExitCodes::ExcludesError) {}
277 explicit ExtrasError(std::vector<std::string> args)
278 :
ExtrasError((args.size() > 1 ?
"The following arguments were not expected: " 279 :
"The following argument was not expected: ") +
280 detail::rjoin(args,
" "),
281 ExitCodes::ExtrasError) {}
289 static ConfigError NotConfigurable(std::string item) {
290 return ConfigError(item +
": This option is not allowed in a configuration file");
298 :
InvalidError(name +
": Too many positional arguments with unlimited expected args", ExitCodes::InvalidError) {
317 #undef CLI11_ERROR_DEF 318 #undef CLI11_ERROR_SIMPLE
All errors derive from this one.
Definition: Error.hpp:63
-h or –help on command line
Definition: Error.hpp:156
Does not output a diagnostic in CLI11_PARSE, but allows to return from main() with a specific error c...
Definition: Error.hpp:169
Definition: Error.hpp:304
Thrown when an excludes option is present.
Definition: Error.hpp:268
Thrown when an option is set to conflicting values (non-vector and multi args, for example) ...
Definition: Error.hpp:86
Thrown when a requires option is missing.
Definition: Error.hpp:261
Usually something like –help-all on command line.
Definition: Error.hpp:162
Thrown when extra values are found in an INI file.
Definition: Error.hpp:285
Thrown when validation fails before parsing.
Definition: Error.hpp:295
Thrown when parsing an INI file and it is missing.
Definition: Error.hpp:175
Construction errors (not in parsing)
Definition: Error.hpp:81
Thrown when an option already exists.
Definition: Error.hpp:128
Thrown when counting a non-existent option.
Definition: Error.hpp:312
This is a successful completion on parsing, supposed to exit.
Definition: Error.hpp:150
Thrown on construction of a bad name.
Definition: Error.hpp:114
Anything that can error in Parse.
Definition: Error.hpp:143
Definition: Option.hpp:206
Thrown when the wrong number of arguments has been received.
Definition: Error.hpp:239
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition: Error.hpp:182
Thrown when a required option is missing.
Definition: Error.hpp:205
Thrown when validation of results fails.
Definition: Error.hpp:198