CPP-AP 3.0.1
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
10#include "ap/util/string.hpp"
11#include "ap/util/typing.hpp"
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
44 explicit parsing_failure(const std::string& message) : argument_parser_exception(message) {}
45
46 static parsing_failure unknown_argument(const std::string_view arg_name) noexcept {
47 return parsing_failure(std::format("Unknown argument [{}].", arg_name));
48 }
49
51 const detail::argument_name& arg_name, const std::weak_ordering ordering
52 ) noexcept {
53 if (std::is_lt(ordering))
54 return parsing_failure(
55 std::format("Not enough values provided for argument [{}]", arg_name.str())
56 );
57
58 if (std::is_gt(ordering))
59 return parsing_failure(
60 std::format("Too many values provided for argument [{}]", arg_name.str())
61 );
62
63 return parsing_failure(
64 std::format("Invalid number of values provided for argument [{}]", arg_name.str())
65 );
66 }
67};
68
71 explicit type_error(const std::string& message) : argument_parser_exception(message) {}
72
74 const detail::argument_name& arg_name, const std::type_info& value_type
75 ) noexcept {
76 return type_error(std::format(
77 "Invalid value type specified for argument [{}] = {}.",
78 arg_name.str(),
79 value_type.name()
80 ));
81 }
82
83 template <typename InvalidType>
84 static type_error invalid_value_type(const detail::argument_name& arg_name) noexcept {
85 return type_error(std::format(
86 "Invalid value type specified for argument [{}] = {}.",
87 arg_name.str(),
88 util::get_demangled_type_name<InvalidType>()
89 ));
90 }
91};
92
95 explicit lookup_failure(const std::string& message) : argument_parser_exception(message) {}
96
97 static lookup_failure argument_not_found(const std::string_view& arg_name) noexcept {
98 return lookup_failure(std::format("Argument with given name [{}] not found.", arg_name));
99 }
100};
101
102} // 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.
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_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
parsing_failure(const std::string &message)
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
Provides common typing utility functions.