CPP-AP 2.2.6
Command-line argument parser for C++20
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1// Copyright (c) 2023-2025 Jakub MusiaƂ
2// This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap).
3// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
6
7#pragma once
8
11
12#include <format>
13
14namespace ap {
15
17struct argument_parser_exception : public std::runtime_error {
19 explicit argument_parser_exception(const std::string& message) : std::runtime_error(message) {}
20};
21
24 explicit invalid_configuration(const std::string& message)
25 : argument_parser_exception(message) {}
26
27 static invalid_configuration invalid_argument_name(
28 const std::string_view arg_name, const std::string_view reason
29 ) noexcept {
31 std::format("Given name [{}] is invalid.\nReason: {}", arg_name, reason)
32 );
33 }
34
35 static invalid_configuration argument_name_used(const detail::argument_name& arg_name
36 ) noexcept {
37 return invalid_configuration(std::format("Given name [{}] already used.", arg_name.str()));
38 }
39};
40
43 explicit parsing_failure(const std::string& message) : argument_parser_exception(message) {}
44
45 static parsing_failure unknown_argument(const std::string_view arg_name) noexcept {
46 return parsing_failure(std::format("Unknown argument [{}].", arg_name));
47 }
48
49 static parsing_failure value_already_set(const detail::argument_name& arg_name) noexcept {
50 return parsing_failure(
51 std::format("Value for argument [{}] has already been set.", arg_name.str())
52 );
53 }
54
55 static parsing_failure invalid_value(
56 const detail::argument_name& arg_name, const std::string& value
57 ) noexcept {
58 return parsing_failure(
59 std::format("Cannot parse value `{}` for argument [{}].", value, arg_name.str())
60 );
61 }
62
63 static parsing_failure invalid_choice(
64 const detail::argument_name& arg_name, const std::string& value
65 ) noexcept {
66 return parsing_failure(std::format(
67 "Value `{}` is not a valid choice for argument [{}].", value, arg_name.str()
68 ));
69 }
70
71 static parsing_failure required_argument_not_parsed(const detail::argument_name& arg_name
72 ) noexcept {
73 return parsing_failure(
74 std::format("No values parsed for a required argument [{}]", arg_name.str())
75 );
76 }
77
78 static parsing_failure argument_deduction_failure(const std::vector<std::string_view>& values
79 ) noexcept {
80 return parsing_failure(
81 std::format("Failed to deduce the argument for values [{}]", detail::join(values))
82 );
83 }
84
85 static parsing_failure invalid_nvalues(
86 const detail::argument_name& arg_name, const std::weak_ordering ordering
87 ) noexcept {
88 if (std::is_lt(ordering))
89 return parsing_failure(
90 std::format("Not enough values provided for optional argument [{}]", arg_name.str())
91 );
92
93 if (std::is_gt(ordering))
94 return parsing_failure(
95 std::format("Too many values provided for optional argument [{}]", arg_name.str())
96 );
97
98 return parsing_failure(
99 std::format("Invalid number of values provided for argument [{}]", arg_name.str())
100 );
101 }
102};
103
107 explicit type_error(const std::string& message) : argument_parser_exception(message) {}
108
109 static type_error invalid_value_type(
110 const detail::argument_name& arg_name, const std::type_info& value_type
111 ) noexcept {
112 return type_error(std::format(
113 "Invalid value type specified for argument [{}] = {}.",
114 arg_name.str(),
115 value_type.name()
116 ));
117 }
118};
119
122 explicit lookup_failure(const std::string& message) : argument_parser_exception(message) {}
123
124 static lookup_failure argument_not_found(const std::string_view& arg_name) noexcept {
125 return lookup_failure(std::format("Argument with given name [{}] not found.", arg_name));
126 }
127};
128
129} // namespace ap
Provides common string utility functions.
Base type for the argument parser functionality errors/exceptions.
argument_parser_exception(const std::string &message)
Structure holding the argument's name.
std::string str(const std::optional< char > flag_char=std::nullopt) const noexcept
Get a string representation of the argument_name.
Exception type used for invalid configuration of an argument parser or its arguments.
Exception type used for element lookup errors.
Exception type used for errors encountered during the argument parsing operation.
Exception type used for type-related errors.