args-parser 6.3.6
Loading...
Searching...
No Matches
groups.hpp
Go to the documentation of this file.
1
2/*
3 SPDX-FileCopyrightText: 2026 Igor Mironchik <igor.mironchik@gmail.com>
4 SPDX-License-Identifier: MIT
5*/
6
12
13#ifndef ARGS__GROUPS_HPP__INCLUDED
14#define ARGS__GROUPS_HPP__INCLUDED
15
16// Args include.
17#include "group_iface.hpp"
18#include "types.hpp"
19
20// C++ include.
21#include <algorithm>
22
23namespace Args
24{
25
26//
27// OnlyOneGroup
28//
29
31class OnlyOneGroup final : public GroupIface
32{
33public:
34 template<typename T>
35 explicit OnlyOneGroup(T &&name,
36 bool required = false)
37 : GroupIface(std::forward<T>(name),
38 required)
39 {
40 }
41
42 virtual ~OnlyOneGroup()
43 {
44 }
45
47 ArgType type() const override
48 {
50 }
51
53 bool isDefined() const override
54 {
55 return std::any_of(children().cbegin(), children().cend(), [](const auto &arg) {
56 return arg->isDefined();
57 });
58 }
59
60protected:
69 StringList &flags,
71 StringList &names) const override
72 {
74
75 for (const auto &arg : details::asConst(children())) {
76 if (arg->isRequired()) {
77 throw BaseException(String(SL("Required argument \""))
78 + arg->name()
79 + SL("\" is not allowed to be in OnlyOne group \"")
80 + name()
81 + SL("\"."));
82 }
83 }
84 }
85
87 void checkCorrectnessAfterParsing() const override
88 {
90
91 ArgIface *defined = nullptr;
92
93 for (const auto &arg : details::asConst(children())) {
94 if (arg->isDefined()) {
95 if (defined) {
96 throw BaseException(String(SL("Only one argument can "
97 "be defined in OnlyOne group \""))
98 + name()
99 + SL("\". ")
100 + SL("Whereas defined \"")
101 + defined->name()
102 + SL("\" and \"")
103 + arg->name()
104 + SL("\"."));
105 } else {
106 defined = arg.get();
107 }
108 }
109 }
110 }
111}; // class OnlyOneGroup
112
113//
114// AllOfGroup
115//
116
118class AllOfGroup final : public GroupIface
119{
120public:
121 template<typename T>
122 explicit AllOfGroup(T &&name,
123 bool required = false)
124 : GroupIface(std::forward<T>(name),
125 required)
126 {
127 }
128
129 virtual ~AllOfGroup()
130 {
131 }
132
134 ArgType type() const override
135 {
136 return ArgType::AllOfGroup;
137 }
138
140 bool isDefined() const override
141 {
142 return !std::any_of(children().cbegin(), children().cend(), [](const auto &arg) {
143 return !arg->isDefined();
144 });
145 }
146
147protected:
156 StringList &flags,
158 StringList &names) const override
159 {
161
162 for (const auto &arg : details::asConst(children())) {
163 if (arg->isRequired()) {
164 throw BaseException(String(SL("Required argument \""))
165 + arg->name()
166 + SL("\" is not allowed to ")
167 + SL("be in AllOf group \"")
168 + name()
169 + SL("\"."));
170 }
171 }
172 }
173
175 void checkCorrectnessAfterParsing() const override
176 {
178
179 const bool defined = std::any_of(children().cbegin(), children().cend(), [](const auto &arg) {
180 return arg->isDefined();
181 });
182
183 const bool all = std::all_of(children().cbegin(), children().cend(), [](const auto &arg) {
184 return arg->isDefined();
185 });
186
187 if (defined && !all) {
188 throw BaseException(String(SL("All arguments in "
189 "AllOf group \""))
190 + name()
191 + SL("\" should be defined."));
192 }
193 }
194}; // class AllOfGroup
195
196//
197// AtLeastOneGroup
198//
199
201class AtLeastOneGroup final : public GroupIface
202{
203public:
204 template<typename T>
205 explicit AtLeastOneGroup(T &&name,
206 bool required = false)
207 : GroupIface(std::forward<T>(name),
208 required)
209 {
210 }
211
213 {
214 }
215
217 ArgType type() const override
218 {
220 }
221
223 bool isDefined() const override
224 {
225 return std::any_of(children().cbegin(), children().cend(), [](const auto &arg) {
226 return arg->isDefined();
227 });
228 }
229
230protected:
239 StringList &flags,
241 StringList &names) const override
242 {
244
245 for (const auto &arg : details::asConst(children())) {
246 if (arg->isRequired()) {
247 throw BaseException(String(SL("Required argument \""))
248 + arg->name()
249 + SL("\" is not allowed to ")
250 + SL("be in AtLeastOne group \"")
251 + name()
252 + SL("\"."));
253 }
254 }
255 }
256}; // class AtLeastOneGroup
257
258} /* namespace Args */
259
260#endif // ARGS__GROUPS_HPP__INCLUDED
bool isDefined() const override
Definition groups.hpp:140
AllOfGroup(T &&name, bool required=false)
Definition groups.hpp:122
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition groups.hpp:154
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Definition groups.hpp:175
ArgType type() const override
Definition groups.hpp:134
virtual ~AllOfGroup()
Definition groups.hpp:129
Interface for arguments.
Definition arg_iface.hpp:29
virtual String name() const =0
AtLeastOneGroup(T &&name, bool required=false)
Definition groups.hpp:205
virtual ~AtLeastOneGroup()
Definition groups.hpp:212
bool isDefined() const override
Definition groups.hpp:223
ArgType type() const override
Definition groups.hpp:217
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition groups.hpp:237
Base exception of the library.
String name() const override
const Arguments & children() const
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
GroupIface(T &&name, bool required=false)
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
void checkCorrectnessBeforeParsing(StringList &flags, StringList &names) const override
Check correctness of the argument before parsing.
Definition groups.hpp:67
OnlyOneGroup(T &&name, bool required=false)
Definition groups.hpp:35
bool isDefined() const override
Definition groups.hpp:53
virtual ~OnlyOneGroup()
Definition groups.hpp:42
ArgType type() const override
Definition groups.hpp:47
void checkCorrectnessAfterParsing() const override
Check correctness of the argument after parsing.
Definition groups.hpp:87
constexpr std::add_const< T >::type & asConst(T &t) noexcept
Adds const to non-const objects.
Definition utils.hpp:45
Definition api.hpp:18
std::string String
String type.
Definition types.hpp:314
ArgType
Type of the argument.
Definition enums.hpp:32
@ AllOfGroup
"All of" group.
Definition enums.hpp:42
@ AtLeastOneGroup
"At least one" group.
Definition enums.hpp:44
@ OnlyOneGroup
"Only one" group.
Definition enums.hpp:40
std::vector< String > StringList
List of strings.
Definition types.hpp:336
#define SL(str)
Definition types.hpp:328