tclap 1.4.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 <list>
31#include <string>
32
33namespace TCLAP {
34
41class ArgGroup : public ArgContainer {
42 friend class CmdLine;
43 friend class CmdLineInterface;
44public:
45 typedef std::list<Arg *> Container;
46 typedef Container::iterator iterator;
47 typedef Container::const_iterator const_iterator;
48
49 virtual ~ArgGroup() {}
50
52 virtual ArgContainer &add(Arg &arg) { return add(&arg); }
53
55 virtual ArgContainer &add(Arg *arg);
56
66 virtual bool validate() = 0;
67
73 virtual bool isRequired() const = 0;
74
82 virtual bool isExclusive() const = 0;
83
93 if (_parser)
94 throw SpecificationException("Arg group can have only one parser");
95 if (parser.attachGroup(*this)) return;
96 // Preserve the legacy path for custom CmdLineInterface implementations.
97 _parser = &parser;
98 for (iterator it = begin(); it != end(); ++it)
99 parser.addToArgList(*it);
100 }
101
105 virtual bool showAsGroup() const { return true; }
106
108 const std::string getName() const;
109
110 iterator begin() { return _args.begin(); }
111 iterator end() { return _args.end(); }
112 const_iterator begin() const { return _args.begin(); }
113 const_iterator end() const { return _args.end(); }
114
115protected:
116 // No direct instantiation
117 ArgGroup() : _parser(0), _args() {}
118
119private:
120 explicit ArgGroup(const ArgGroup &);
121 ArgGroup &operator=(const ArgGroup &); // no copy
122
123protected:
126};
127
134public:
135 inline bool validate();
136 bool isExclusive() const { return true; }
137 ArgContainer &add(Arg &arg) { return add(&arg); }
139 if (arg->isRequired()) {
141 "Required arguments are not allowed"
142 " in an exclusive grouping.",
143 arg->longID());
144 }
145
146 return ArgGroup::add(arg);
147 }
148
149protected:
151 explicit ExclusiveArgGroup(CmdLineInterface &parser) { parser.add(*this); }
152};
153
158public:
160 explicit EitherOf(CmdLineInterface &parser) : ExclusiveArgGroup(parser) {}
161
162 bool isRequired() const { return false; }
163};
164
169class OneOf : public ExclusiveArgGroup {
170public:
171 OneOf() {}
172 explicit OneOf(CmdLineInterface &parser) : ExclusiveArgGroup(parser) {}
173
174 bool isRequired() const { return true; }
175};
176
183class AnyOf : public ArgGroup {
184public:
185 AnyOf() {}
186 explicit AnyOf(CmdLineInterface &parser) { parser.add(*this); }
187
188 bool validate() { return false; /* All good */ }
189 bool isExclusive() const { return false; }
190 bool isRequired() const { return false; }
191};
192
194 for (iterator it = begin(); it != end(); it++) {
195 if (arg->conflictsWith(**it)) {
197 "Argument with same flag/name already exists!", arg->longID());
198 }
199 }
200
201 Container members(_args);
202 members.push_back(arg);
203 if (_parser) {
204 _parser->addToArgList(arg);
205 }
206 _args.swap(members);
207
208 return *this;
209}
210
212 Arg *arg = NULL;
213 std::string flag;
214
215 for (const_iterator it = begin(); it != end(); ++it) {
216 if ((*it)->isSet()) {
217 if (arg != NULL && arg != *it) {
218 // We found a matching argument, but one was
219 // already found previously.
221 "Only one is allowed.",
222 flag + " AND " + (*it)->setBy() + " provided.");
223 }
224
225 arg = *it;
226 flag = arg->setBy();
227 }
228 }
229
230 return isRequired() && !arg;
231}
232
233inline const std::string ArgGroup::getName() const {
234 std::string name;
235 std::string sep = "{"; // TODO: this should change for
236 // non-exclusive arg groups
237 for (const_iterator it = begin(); it != end(); ++it) {
238 name += sep + (*it)->getName();
239 sep = " | ";
240 }
241
242 return name + '}';
243}
244
246inline int CountVisibleArgs(const ArgGroup &g) {
247 int visible = 0;
248 for (ArgGroup::const_iterator it = g.begin(); it != g.end(); ++it) {
249 if ((*it)->visibleInHelp()) {
250 visible++;
251 }
252 }
253
254 return visible;
255}
256
257} // namespace TCLAP
258
259#endif // TCLAP_ARG_GROUP_H
Groups arguments for presentation without imposing a selection rule.
Definition ArgGroup.h:183
bool isRequired() const
Returns true if this argument group is required.
Definition ArgGroup.h:190
bool isExclusive() const
Returns true if this argument group is exclusive.
Definition ArgGroup.h:189
AnyOf(CmdLineInterface &parser)
Definition ArgGroup.h:186
bool validate()
Validates that the constraints of the ArgGroup are satisfied.
Definition ArgGroup.h:188
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:41
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:92
virtual ~ArgGroup()
Definition ArgGroup.h:49
CmdLineInterface * _parser
Definition ArgGroup.h:124
iterator begin()
Definition ArgGroup.h:110
const_iterator begin() const
Definition ArgGroup.h:112
std::list< Arg * > Container
Definition ArgGroup.h:45
Container::const_iterator const_iterator
Definition ArgGroup.h:47
iterator end()
Definition ArgGroup.h:111
const_iterator end() const
Definition ArgGroup.h:113
Container _args
Definition ArgGroup.h:125
const std::string getName() const
Returns the argument group's name.
Definition ArgGroup.h:233
virtual ArgContainer & add(Arg &arg)
Add an argument to this arg group.
Definition ArgGroup.h:52
virtual bool showAsGroup() const
If arguments in this group should show up as grouped in help.
Definition ArgGroup.h:105
virtual bool validate()=0
Validates that the constraints of the ArgGroup are satisfied.
Container::iterator iterator
Definition ArgGroup.h:46
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:66
const std::string & setBy() const
Returns the value specified to set this flag (like -a or –all).
Definition Arg.h:364
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:581
bool conflictsWith(const Arg &a) const
Checks declared registration conflicts: the same object, the same name, or the same nonempty short fl...
Definition Arg.h:730
const std::string & getName() const
Returns the argument name.
Definition Arg.h:745
virtual bool isRequired() const
Indicates whether the argument is required.
Definition Arg.h:747
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual bool attachGroup(ArgGroup &)
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
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:85
Implements a group of arguments where at most one can be selected.
Definition ArgGroup.h:157
bool isRequired() const
Returns true if this argument group is required.
Definition ArgGroup.h:162
EitherOf(CmdLineInterface &parser)
Definition ArgGroup.h:160
Implements common functionality for exclusive argument groups.
Definition ArgGroup.h:133
bool isExclusive() const
Returns true if this argument group is exclusive.
Definition ArgGroup.h:136
ArgContainer & add(Arg &arg)
Add an argument to this arg group.
Definition ArgGroup.h:137
ExclusiveArgGroup(CmdLineInterface &parser)
Definition ArgGroup.h:151
bool validate()
Validates that the constraints of the ArgGroup are satisfied.
Definition ArgGroup.h:211
ArgContainer & add(Arg *arg)
Add an argument to this arg group.
Definition ArgGroup.h:138
Implements a group of arguments where exactly one must be selected.
Definition ArgGroup.h:169
bool isRequired() const
Returns true if this argument group is required.
Definition ArgGroup.h:174
OneOf(CmdLineInterface &parser)
Definition ArgGroup.h:172
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
Definition Arg.h:52
int CountVisibleArgs(const ArgGroup &g)
Definition ArgGroup.h:246