CPP-AP 2.7.0
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
12
13#include <format>
14
15namespace ap {
16
18struct argument_parser_exception : public std::runtime_error {
20 explicit argument_parser_exception(const std::string& message) : std::runtime_error(message) {}
21};
22
25 explicit invalid_configuration(const std::string& message)
26 : argument_parser_exception(message) {}
27
29 const std::string_view arg_name, const std::string_view reason
30 ) noexcept {
32 std::format("Given name [{}] is invalid.\nReason: {}", arg_name, reason)
33 );
34 }
35
37 ) noexcept {
38 return invalid_configuration(std::format("Given name [{}] already used.", arg_name.str()));
39 }
40
41 struct positional {
43 const detail::argument_name& required_arg_name,
44 const detail::argument_name& non_required_arg_name
45 ) noexcept {
46 return invalid_configuration(std::format(
47 "Required positional argument [{}] cannot be defined after a non-required "
48 "positional argument [{}].",
49 required_arg_name.str(),
50 non_required_arg_name.str()
51 ));
52 }
53 };
54};
55
58 explicit parsing_failure(const std::string& message) : argument_parser_exception(message) {}
59
60 static parsing_failure unknown_argument(const std::string_view arg_name) noexcept {
61 return parsing_failure(std::format("Unknown argument [{}].", arg_name));
62 }
63
64 static parsing_failure value_already_set(const detail::argument_name& arg_name) noexcept {
65 return parsing_failure(
66 std::format("Value for argument [{}] has already been set.", arg_name.str())
67 );
68 }
69
71 const detail::argument_name& arg_name, const std::string& value
72 ) noexcept {
73 return parsing_failure(
74 std::format("Cannot parse value `{}` for argument [{}].", value, arg_name.str())
75 );
76 }
77
79 const detail::argument_name& arg_name, const std::string& value
80 ) noexcept {
81 return parsing_failure(std::format(
82 "Value `{}` is not a valid choice for argument [{}].", value, arg_name.str()
83 ));
84 }
85
87 ) noexcept {
88 return parsing_failure(
89 std::format("No values parsed for a required argument [{}]", arg_name.str())
90 );
91 }
92
93 static parsing_failure argument_deduction_failure(const std::vector<std::string>& values
94 ) noexcept {
95 return parsing_failure(
96 std::format("Failed to deduce the argument for values [{}]", detail::join(values))
97 );
98 }
99
101 const detail::argument_name& arg_name, const std::weak_ordering ordering
102 ) noexcept {
103 if (std::is_lt(ordering))
104 return parsing_failure(
105 std::format("Not enough values provided for optional argument [{}]", arg_name.str())
106 );
107
108 if (std::is_gt(ordering))
109 return parsing_failure(
110 std::format("Too many values provided for optional argument [{}]", arg_name.str())
111 );
112
113 return parsing_failure(
114 std::format("Invalid number of values provided for argument [{}]", arg_name.str())
115 );
116 }
117};
118
122 explicit type_error(const std::string& message) : argument_parser_exception(message) {}
123
125 const detail::argument_name& arg_name, const std::type_info& value_type
126 ) noexcept {
127 return type_error(std::format(
128 "Invalid value type specified for argument [{}] = {}.",
129 arg_name.str(),
130 value_type.name()
131 ));
132 }
133
134 template <typename InvalidType>
135 static type_error invalid_value_type(const detail::argument_name& arg_name) noexcept {
136 return type_error(std::format(
137 "Invalid value type specified for argument [{}] = {}.",
138 arg_name.str(),
139 detail::get_demangled_type_name<InvalidType>()
140 ));
141 }
142};
143
146 explicit lookup_failure(const std::string& message) : argument_parser_exception(message) {}
147
148 static lookup_failure argument_not_found(const std::string_view& arg_name) noexcept {
149 return lookup_failure(std::format("Argument with given name [{}] not found.", arg_name));
150 }
151};
152
153} // namespace ap
std::string join(const R &range, const std::string_view delimiter=", ")
Joins elements of a range into a single string with a delimiter.
Definition utility.hpp:17
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.
static invalid_configuration required_after_non_required(const detail::argument_name &required_arg_name, const detail::argument_name &non_required_arg_name) noexcept
Exception type used for invalid configuration of an argument parser or its arguments.
static invalid_configuration argument_name_used(const detail::argument_name &arg_name) noexcept
invalid_configuration(const std::string &message)
static invalid_configuration invalid_argument_name(const std::string_view arg_name, const std::string_view reason) noexcept
Exception type used for element lookup errors.
lookup_failure(const std::string &message)
static lookup_failure argument_not_found(const std::string_view &arg_name) noexcept
Exception type used for errors encountered during the argument parsing operation.
static parsing_failure invalid_value(const detail::argument_name &arg_name, const std::string &value) noexcept
static parsing_failure invalid_nvalues(const detail::argument_name &arg_name, const std::weak_ordering ordering) noexcept
static parsing_failure unknown_argument(const std::string_view arg_name) noexcept
static parsing_failure required_argument_not_parsed(const detail::argument_name &arg_name) noexcept
static parsing_failure invalid_choice(const detail::argument_name &arg_name, const std::string &value) noexcept
parsing_failure(const std::string &message)
static parsing_failure argument_deduction_failure(const std::vector< std::string > &values) noexcept
static parsing_failure value_already_set(const detail::argument_name &arg_name) noexcept
Exception type used for type-related errors.
type_error(const std::string &message)
static type_error invalid_value_type(const detail::argument_name &arg_name, const std::type_info &value_type) noexcept
static type_error invalid_value_type(const detail::argument_name &arg_name) noexcept