tclap 1.6.0
ArgGroup.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: ArgGroup.h
6 *
7 * Copyright (c) 2017 Google LLC.
8 * All rights reserved.
9 *
10 * See the file COPYING in the top directory of this distribution for
11 * more information.
12 *
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *****************************************************************************/
22
23#ifndef TCLAP_ARG_GROUP_H
24#define TCLAP_ARG_GROUP_H
25
26#include <tclap/Arg.h>
27#include <tclap/ArgContainer.h>
29
30#include <algorithm>
31#include <list>
32#include <string>
33
34namespace TCLAP {
35
42class ArgGroup : public ArgContainer {
43 friend class CmdLine;
44 friend class CmdLineInterface;
45public:
46 using Container = std::list<Arg *>;
47 using iterator = Container::iterator;
48 using const_iterator = Container::const_iterator;
49
50 ~ArgGroup() override = default;
51
53 ArgContainer &add(Arg &arg) override { return add(&arg); }
54
56 ArgContainer &add(Arg *arg) override;
57
67 virtual bool validate() = 0;
68
74 [[nodiscard]] virtual bool isRequired() const = 0;
75
83 [[nodiscard]] virtual bool isExclusive() const = 0;
84
94 if (_parser)
95 throw SpecificationException("Arg group can have only one parser");
96 if (parser.attachGroup(*this)) return;
97 // Preserve the legacy path for custom CmdLineInterface implementations.
98 _parser = &parser;
99 for (Arg *arg : *this) {
100 parser.addToArgList(arg);
101 }
102 }
103
107 [[nodiscard]] virtual bool showAsGroup() const { return true; }
108
110 [[nodiscard]] const std::string getName() const;
111
112 iterator begin() noexcept { return _args.begin(); }
113 iterator end() noexcept { return _args.end(); }
114 [[nodiscard]] const_iterator begin() const noexcept { return _args.begin(); }
115 [[nodiscard]] const_iterator end() const noexcept { return _args.end(); }
116
117protected:
118 // No direct instantiation
119 ArgGroup() : _parser(nullptr), _args() {}
120
121public:
122 ArgGroup(const ArgGroup &) = delete;
123 ArgGroup &operator=(const ArgGroup &) = delete; // no copy
124
125protected:
128};
129
136public:
137 bool validate() override;
138 [[nodiscard]] bool isExclusive() const override { return true; }
139 ArgContainer &add(Arg &arg) override { return add(&arg); }
140 ArgContainer &add(Arg *arg) override {
141 if (arg->isRequired()) {
143 "Required arguments are not allowed"
144 " in an exclusive grouping.",
145 arg->longID());
146 }
147
148 return ArgGroup::add(arg);
149 }
150
151protected:
152 ExclusiveArgGroup() = default;
153 explicit ExclusiveArgGroup(CmdLineInterface &parser) { parser.add(*this); }
154};
155
160public:
161 EitherOf() = default;
162 explicit EitherOf(CmdLineInterface &parser) : ExclusiveArgGroup(parser) {}
163
164 [[nodiscard]] bool isRequired() const override { return false; }
165};
166
171class OneOf : public ExclusiveArgGroup {
172public:
173 OneOf() = default;
174 explicit OneOf(CmdLineInterface &parser) : ExclusiveArgGroup(parser) {}
175
176 [[nodiscard]] bool isRequired() const override { return true; }
177};
178
185class AnyOf : public ArgGroup {
186public:
187 AnyOf() = default;
188 explicit AnyOf(CmdLineInterface &parser) { parser.add(*this); }
189
190 bool validate() override { return false; /* All good */ }
191 [[nodiscard]] bool isExclusive() const override { return false; }
192 [[nodiscard]] bool isRequired() const override { return false; }
193};
194
196 if (std::ranges::any_of(*this, [arg](const Arg *existing) {
197 return arg->conflictsWith(*existing);
198 })) {
200 "Argument with same flag/name already exists!", arg->longID());
201 }
202
203 Container members(_args);
204 members.push_back(arg);
205 if (_parser) {
206 _parser->addToArgList(arg);
207 }
208 _args.swap(members);
209
210 return *this;
211}
212
214 Arg *arg = nullptr;
215 std::string flag;
216
217 for (Arg *candidate : *this) {
218 if (candidate->isSet()) {
219 if (arg != nullptr && arg != candidate) {
220 // We found a matching argument, but one was
221 // already found previously.
223 "Only one is allowed.",
224 flag + " AND " + candidate->setBy() + " provided.");
225 }
226
227 arg = candidate;
228 flag = arg->setBy();
229 }
230 }
231
232 return isRequired() && !arg;
233}
234
235inline const std::string ArgGroup::getName() const {
236 std::string name;
237 std::string sep = "{"; // TODO: this should change for
238 // non-exclusive arg groups
239 for (const Arg *arg : *this) {
240 name += sep + arg->getName();
241 sep = " | ";
242 }
243
244 return name + '}';
245}
246
248inline int CountVisibleArgs(const ArgGroup &g) {
249 return static_cast<int>(std::ranges::count_if(
250 g, [](const Arg *arg) { return arg->visibleInHelp(); }));
251}
252
253} // namespace TCLAP
254
255#endif // TCLAP_ARG_GROUP_H
Groups arguments for presentation without imposing a selection rule.
Definition ArgGroup.h:185
AnyOf()=default
bool isExclusive() const override
Returns true if this argument group is exclusive.
Definition ArgGroup.h:191
AnyOf(CmdLineInterface &parser)
Definition ArgGroup.h:188
bool validate() override
Validates that the constraints of the ArgGroup are satisfied.
Definition ArgGroup.h:190
bool isRequired() const override
Returns true if this argument group is required.
Definition ArgGroup.h:192
Interface that allows adding an Arg to a "container".
ArgGroup is the base class for implementing groups of arguments that are mutually exclusive (it repla...
Definition ArgGroup.h:42
virtual bool isExclusive() const =0
Returns true if this argument group is exclusive.
void setParser(CmdLineInterface &parser)
Used by the parser to connect itself to this arg group.
Definition ArgGroup.h:93
CmdLineInterface * _parser
Definition ArgGroup.h:126
iterator begin() noexcept
Definition ArgGroup.h:112
ArgGroup & operator=(const ArgGroup &)=delete
iterator end() noexcept
Definition ArgGroup.h:113
const_iterator end() const noexcept
Definition ArgGroup.h:115
Container::const_iterator const_iterator
Definition ArgGroup.h:48
const_iterator begin() const noexcept
Definition ArgGroup.h:114
Container _args
Definition ArgGroup.h:127
Container::iterator iterator
Definition ArgGroup.h:47
const std::string getName() const
Returns the argument group's name.
Definition ArgGroup.h:235
virtual bool showAsGroup() const
If arguments in this group should show up as grouped in help.
Definition ArgGroup.h:107
ArgContainer & add(Arg &arg) override
Add an argument to this arg group.
Definition ArgGroup.h:53
std::list< Arg * > Container
Definition ArgGroup.h:46
virtual bool validate()=0
Validates that the constraints of the ArgGroup are satisfied.
ArgGroup(const ArgGroup &)=delete
~ArgGroup() override=default
virtual bool isRequired() const =0
Returns true if this argument group is required.
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:69
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:603
bool conflictsWith(const Arg &a) const noexcept
Checks declared registration conflicts: the same object, the same name, or the same nonempty short fl...
Definition Arg.h:745
const std::string & setBy() const noexcept
Returns the value specified to set this flag (like -a or –all).
Definition Arg.h:366
const std::string & getName() const noexcept
Returns the argument name.
Definition Arg.h:759
virtual bool visibleInHelp() const
Returns true if this Arg is visible in the help output.
Definition Arg.h:447
virtual bool isRequired() const
Indicates whether the argument is required.
Definition Arg.h:761
The base class that manages the command line definition and passes along the parsing to the appropria...
ArgContainer & add(Arg &a) override=0
Adds an argument.
virtual bool attachGroup(ArgGroup &)
virtual void addToArgList(Arg *a)=0
Thrown from CmdLine when the arguments on the command line are not properly specified,...
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition CmdLine.h:86
Implements a group of arguments where at most one can be selected.
Definition ArgGroup.h:159
bool isRequired() const override
Returns true if this argument group is required.
Definition ArgGroup.h:164
EitherOf(CmdLineInterface &parser)
Definition ArgGroup.h:162
EitherOf()=default
Implements common functionality for exclusive argument groups.
Definition ArgGroup.h:135
ArgContainer & add(Arg &arg) override
Add an argument to this arg group.
Definition ArgGroup.h:139
bool validate() override
Validates that the constraints of the ArgGroup are satisfied.
Definition ArgGroup.h:213
ArgContainer & add(Arg *arg) override
Add an argument to this arg group.
Definition ArgGroup.h:140
ExclusiveArgGroup(CmdLineInterface &parser)
Definition ArgGroup.h:153
bool isExclusive() const override
Returns true if this argument group is exclusive.
Definition ArgGroup.h:138
Implements a group of arguments where exactly one must be selected.
Definition ArgGroup.h:171
OneOf()=default
OneOf(CmdLineInterface &parser)
Definition ArgGroup.h:174
bool isRequired() const override
Returns true if this argument group is required.
Definition ArgGroup.h:176
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
Definition Arg.h:54
int CountVisibleArgs(const ArgGroup &g)
Definition ArgGroup.h:248