ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
parse_exception.h
1/*
2 * Copyright 2025 Dominik Czekai
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18#include <stdexcept>
19#include <typeinfo>
20#include <format>
21#include "commands/argument.h"
22#include "commands/argument_group.h"
23
24namespace cli::parsing
25{
27class ParseException : public std::runtime_error
28{
29public:
34 ParseException(const std::string &message, const std::string &input, const cli::commands::ArgumentBase &argument)
35 : std::runtime_error(message), input(input), argument(argument)
36 {
37 }
38
42 ParseException(const std::string &input, const cli::commands::ArgumentBase &argument)
43 : ParseException(std::format("Failed to parse input '{}' for argument: {}", input, argument.getName()), input, argument)
44 {
45 }
46
49 const std::string &getInput() const noexcept { return input; }
50
53 const cli::commands::ArgumentBase &getArgument() const noexcept { return argument; }
54
55private:
56 std::string input;
57 const cli::commands::ArgumentBase &argument;
58};
59
61class TypeParseException : public std::runtime_error
62{
63public:
68 TypeParseException(const std::string &message, const std::string &input, const std::type_info &targetType)
69 : std::runtime_error(message), input(input), targetType(targetType)
70 {
71 }
72
76 TypeParseException(const std::string &input, const std::type_info &targetType)
77 : TypeParseException(std::format("Could not parse '{}' to type '{}'", input, targetType.name()), input, targetType)
78 {
79 }
80
83 const std::string &getInput() const noexcept { return input; }
84
87 const std::type_info &getTargetType() const noexcept { return targetType; }
88
89private:
90 std::string input;
91 const std::type_info &targetType;
92};
93
95class GroupParseException : public std::runtime_error
96{
97public:
101 GroupParseException(const std::string &message, const cli::commands::ArgumentGroup &argumentGroup)
102 : std::runtime_error(message), argumentGroup(argumentGroup)
103 {
104 }
105
109 : GroupParseException(std::format("Failed to parse argument group"), argumentGroup)
110 {
111 }
112
115 const cli::commands::ArgumentGroup &getArgumentGroup() const noexcept { return argumentGroup; }
116
117private:
118 const cli::commands::ArgumentGroup &argumentGroup;
119};
120
121} // namespace cli::parsing
Base class for command-line arguments.
Definition argument.h:41
Base class for command-line argument groups.
Definition argument_group.h:30
Exception thrown when parsing a group of arguments fails.
Definition parse_exception.h:96
GroupParseException(const std::string &message, const cli::commands::ArgumentGroup &argumentGroup)
Construct a GroupParseException with a message and argument group.
Definition parse_exception.h:101
const cli::commands::ArgumentGroup & getArgumentGroup() const noexcept
Gets the argument group that failed to parse.
Definition parse_exception.h:115
GroupParseException(const cli::commands::ArgumentGroup &argumentGroup)
Construct a GroupParseException with default message and argument group.
Definition parse_exception.h:108
Exception thrown when parsing for an argument fails.
Definition parse_exception.h:28
ParseException(const std::string &input, const cli::commands::ArgumentBase &argument)
Construct a ParseException with default message, input string, and argument.
Definition parse_exception.h:42
const cli::commands::ArgumentBase & getArgument() const noexcept
Gets the argument that failed to parse.
Definition parse_exception.h:53
ParseException(const std::string &message, const std::string &input, const cli::commands::ArgumentBase &argument)
Construct a ParseException with a message, input string, and argument.
Definition parse_exception.h:34
const std::string & getInput() const noexcept
Gets the input string that failed to parse.
Definition parse_exception.h:49
Exception thrown when the input string cannot be parsed to the needed type for an argument.
Definition parse_exception.h:62
const std::type_info & getTargetType() const noexcept
Gets the target type that the input couldn't be parsed to.
Definition parse_exception.h:87
TypeParseException(const std::string &input, const std::type_info &targetType)
Construct a TypeParseException with default message, input string, and target type.
Definition parse_exception.h:76
TypeParseException(const std::string &message, const std::string &input, const std::type_info &targetType)
Construct a TypeParseException with a message, input string, and target type.
Definition parse_exception.h:68
const std::string & getInput() const noexcept
Gets the input string that failed to parse.
Definition parse_exception.h:83