Debian Codegraph @

podofo/3rdparty/tclap/ArgGroup.h

raw symbols SCIP
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>
28 #include <tclap/CmdLineInterface.h>
29
30 #include <list>
31 #include <string>
32
33 namespace TCLAP {
34
35 /**
36 * ArgGroup is the base class for implementing groups of arguments
37 * that are mutually exclusive (it replaces the deprecated xor
38 * handler). It is not expected to be used directly, rather one of the
39 * EitherOf or OneOf derived classes are used.
40 */
41 class ArgGroup : public ArgContainer {
42 public:
43 typedef std::list<Arg *> Container;
44 typedef Container::iterator iterator;
45 typedef Container::const_iterator const_iterator;
46
47 virtual ~ArgGroup() {}
48
49 /// Add an argument to this arg group
50 virtual ArgContainer &add(Arg &arg) { return add(&arg); }
51
52 /// Add an argument to this arg group
53 virtual ArgContainer &add(Arg *arg);
54
55 /**
56 * Validates that the constraints of the ArgGroup are satisfied.
57 *
58 * @internal
59 * Throws an CmdLineParseException if there is an issue (except
60 * missing required argument, in which case true is returned).
61 *
62 * @retval true iff a required argument was missing.
63 */
64 virtual bool validate() = 0;
65
66 /**
67 * Returns true if this argument group is required
68 *
69 * @internal
70 */
71 virtual bool isRequired() const = 0;
72
73 /**
74 * Returns true if this argument group is exclusive.
75 *
76 * @internal
77 * Being exclusive means there is a constraint so that some
78 * arguments cannot be selected at the same time.
79 */
80 virtual bool isExclusive() const = 0;
81
82 /**
83 * Used by the parser to connect itself to this arg group.
84 *
85 * @internal
86 * This is needed so that existing and subsequently added args (in
87 * this arg group) are also added to the parser (and checked for
88 * consistency with other args).
89 */
90 void setParser(CmdLineInterface &parser) {
91 if (_parser) {
92 throw SpecificationException("Arg group can have only one parser");
93 }
94
95 _parser = &parser;
96 for (iterator it = begin(); it != end(); ++it) {
97 parser.addToArgList(*it);
98 }
99 }
100
101 /**
102 * If arguments in this group should show up as grouped in help.
103 */
104 virtual bool showAsGroup() const { return true; }
105
106 /// Returns the argument group's name.
107 const std::string getName() const;
108
109 iterator begin() { return _args.begin(); }
110 iterator end() { return _args.end(); }
111 const_iterator begin() const { return _args.begin(); }
112 const_iterator end() const { return _args.end(); }
113
114 protected:
115 // No direct instantiation
116 ArgGroup() : _parser(0), _args() {}
117
118 private:
119 explicit ArgGroup(const ArgGroup &);
120 ArgGroup &operator=(const ArgGroup &); // no copy
121
122 protected:
123 CmdLineInterface *_parser;
124 Container _args;
125 };
126
127 /**
128 * Implements common functionality for exclusive argument groups.
129 *
130 * @internal
131 */
132 class ExclusiveArgGroup : public ArgGroup {
133 public:
134 inline bool validate();
135 bool isExclusive() const { return true; }
136 ArgContainer &add(Arg &arg) { return add(&arg); }
137 ArgContainer &add(Arg *arg) {
138 if (arg->isRequired()) {
139 throw SpecificationException(
140 "Required arguments are not allowed"
141 " in an exclusive grouping.",
142 arg->longID());
143 }
144
145 return ArgGroup::add(arg);
146 }
147
148 protected:
149 ExclusiveArgGroup() {}
150 explicit ExclusiveArgGroup(CmdLineInterface &parser) { parser.add(*this); }
151 };
152
153 /**
154 * Implements a group of arguments where at most one can be selected.
155 */
156 class EitherOf : public ExclusiveArgGroup {
157 public:
158 EitherOf() {}
159 explicit EitherOf(CmdLineInterface &parser) : ExclusiveArgGroup(parser) {}
160
161 bool isRequired() const { return false; }
162 };
163
164 /**
165 * Implements a group of arguments where exactly one must be
166 * selected. This corresponds to the deprecated "xoradd".
167 */
168 class OneOf : public ExclusiveArgGroup {
169 public:
170 OneOf() {}
171 explicit OneOf(CmdLineInterface &parser) : ExclusiveArgGroup(parser) {}
172
173 bool isRequired() const { return true; }
174 };
175
176 /**
177 * Implements a group of arguments where any combination is possible
178 * (including all or none). This is mostly used in case one optional
179 * argument allows additional arguments to be specified (for example
180 * [-c [-de] [-n <int>]]).
181 */
182 class AnyOf : public ArgGroup {
183 public:
184 AnyOf() {}
185 explicit AnyOf(CmdLineInterface &parser) { parser.add(*this); }
186
187 bool validate() { return false; /* All good */ }
188 bool isExclusive() const { return false; }
189 bool isRequired() const { return false; }
190 };
191
192 inline ArgContainer &ArgGroup::add(Arg *arg) {
193 for (iterator it = begin(); it != end(); it++) {
194 if (*arg == **it) {
195 throw SpecificationException(
196 "Argument with same flag/name already exists!", arg->longID());
197 }
198 }
199
200 _args.push_back(arg);
201 if (_parser) {
202 _parser->addToArgList(arg);
203 }
204
205 return *this;
206 }
207
208 inline bool ExclusiveArgGroup::validate() {
209 Arg *arg = NULL;
210 std::string flag;
211
212 for (const_iterator it = begin(); it != end(); ++it) {
213 if ((*it)->isSet()) {
214 if (arg != NULL && !(*arg == **it)) {
215 // We found a matching argument, but one was
216 // already found previously.
217 throw CmdLineParseException(
218 "Only one is allowed.",
219 flag + " AND " + (*it)->setBy() + " provided.");
220 }
221
222 arg = *it;
223 flag = arg->setBy();
224 }
225 }
226
227 return isRequired() && !arg;
228 }
229
230 inline const std::string ArgGroup::getName() const {
231 std::string name;
232 std::string sep = "{"; // TODO: this should change for
233 // non-exclusive arg groups
234 for (const_iterator it = begin(); it != end(); ++it) {
235 name += sep + (*it)->getName();
236 sep = " | ";
237 }
238
239 return name + '}';
240 }
241
242 /// @internal
243 inline int CountVisibleArgs(const ArgGroup &g) {
244 int visible = 0;
245 for (ArgGroup::const_iterator it = g.begin(); it != g.end(); ++it) {
246 if ((*it)->visibleInHelp()) {
247 visible++;
248 }
249 }
250
251 return visible;
252 }
253
254 } // namespace TCLAP
255
256 #endif // TCLAP_ARG_GROUP_H