73class Error :
public std::runtime_error {
75 std::string error_name{
"Error"};
78 CLI11_NODISCARD
int get_exit_code()
const {
return actual_exit_code; }
80 CLI11_NODISCARD std::string get_name()
const {
return error_name; }
82 Error(std::string name, std::string msg,
int exit_code =
static_cast<int>(ExitCodes::BaseClass))
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)
86 : Error(std::move(name), std::move(msg),
static_cast<int>(exit_code)) {}
147 explicit OptionAlreadyAdded(std::string name)
148 : OptionAlreadyAdded(name +
" is already added", ExitCodes::OptionAlreadyAdded) {}
149 static OptionAlreadyAdded Requires(std::string name, std::string other) {
150 return {name +
" requires " + other, ExitCodes::OptionAlreadyAdded};
152 static OptionAlreadyAdded Excludes(std::string name, std::string other) {
153 return {name +
" excludes " + other, ExitCodes::OptionAlreadyAdded};
208 CLI11_ERROR_SIMPLE(ConversionError)
209 ConversionError(std::string member, std::string name)
210 : ConversionError(
"The value " + member +
" is not an allowed value for " + name) {}
211 ConversionError(std::string name, std::vector<std::string> results)
212 : ConversionError(
"Could not convert: " + name +
" = " + detail::join(results)) {}
213 static ConversionError TooManyInputsFlag(std::string name) {
214 return ConversionError(name +
": too many inputs for a flag");
216 static ConversionError TrueFalse(std::string name) {
217 return ConversionError(name +
": Should be true/false or a number");
231 explicit RequiredError(std::string name) : RequiredError(name +
" is required", ExitCodes::RequiredError) {}
232 static RequiredError Subcommand(std::size_t min_subcom) {
233 if(min_subcom == 1) {
234 return RequiredError(
"A subcommand");
236 return {
"Requires at least " + std::to_string(min_subcom) +
" subcommands", ExitCodes::RequiredError};
239 Option(std::size_t min_option, std::size_t max_option, std::size_t used,
const std::string &option_list) {
240 if((min_option == 1) && (max_option == 1) && (used == 0))
241 return RequiredError(
"Exactly 1 option from [" + option_list +
"]");
242 if((min_option == 1) && (max_option == 1) && (used > 1)) {
243 return {
"Exactly 1 option from [" + option_list +
"] is required but " + std::to_string(used) +
245 ExitCodes::RequiredError};
247 if((min_option == 1) && (used == 0))
248 return RequiredError(
"At least 1 option from [" + option_list +
"]");
249 if(used < min_option) {
250 return {
"Requires at least " + std::to_string(min_option) +
" options used but only " +
251 std::to_string(used) +
" were given from [" + option_list +
"]",
252 ExitCodes::RequiredError};
255 return {
"Requires at most 1 options be given from [" + option_list +
"]", ExitCodes::RequiredError};
257 return {
"Requires at most " + std::to_string(max_option) +
" options be used but " + std::to_string(used) +
258 " were given from [" + option_list +
"]",
259 ExitCodes::RequiredError};
266 CLI11_ERROR_SIMPLE(ArgumentMismatch)
267 ArgumentMismatch(std::string name,
int expected, std::size_t received)
268 : ArgumentMismatch(expected > 0 ? (
"Expected exactly " + std::to_string(expected) +
" arguments to " + name +
269 ", got " + std::to_string(received))
270 : (
"Expected at least " + std::to_string(-expected) +
" arguments to " + name +
271 ", got " + std::to_string(received)),
272 ExitCodes::ArgumentMismatch) {}
274 static ArgumentMismatch AtLeast(std::string name,
int num, std::size_t received) {
275 return ArgumentMismatch(name +
": At least " + std::to_string(num) +
" required but received " +
276 std::to_string(received));
278 static ArgumentMismatch AtMost(std::string name,
int num, std::size_t received) {
279 return ArgumentMismatch(name +
": At most " + std::to_string(num) +
" required but received " +
280 std::to_string(received));
282 static ArgumentMismatch TypedAtLeast(std::string name,
int num, std::string type) {
283 return ArgumentMismatch(name +
": " + std::to_string(num) +
" required " + type +
" missing");
285 static ArgumentMismatch FlagOverride(std::string name) {
286 return ArgumentMismatch(name +
" was given a disallowed flag override");
288 static ArgumentMismatch PartialType(std::string name,
int num, std::string type) {
289 return ArgumentMismatch(name +
": " + type +
" only partially specified: " + std::to_string(num) +
290 " required for each element");
311 explicit ExtrasError(std::vector<std::string> args)
312 : ExtrasError((args.size() > 1 ?
"The following arguments were not expected: "
313 :
"The following argument was not expected: ") +
314 detail::join(args,
" "),
315 ExitCodes::ExtrasError) {}
316 ExtrasError(
const std::string &name, std::vector<std::string> args)
317 : ExtrasError((name.empty() ? std::string{} : name +
": ") +
318 (args.size() > 1 ?
"The following arguments were not expected: "
319 :
"The following argument was not expected: ") +
320 detail::join(args,
" "),
321 ExitCodes::ExtrasError) {}