| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 50 |
virtual ArgContainer &add(Arg &arg) { return add(&arg); } |
| 51 |
|
| 52 |
|
| 53 |
virtual ArgContainer &add(Arg *arg); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
virtual bool validate() = 0; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
virtual bool isRequired() const = 0; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
virtual bool isExclusive() const = 0; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 103 |
|
| 104 |
virtual bool showAsGroup() const { return true; } |
| 105 |
|
| 106 |
|
| 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 |
|
| 116 |
ArgGroup() : _parser(0), _args() {} |
| 117 |
|
| 118 |
private: |
| 119 |
explicit ArgGroup(const ArgGroup &); |
| 120 |
ArgGroup &operator=(const ArgGroup &); |
| 121 |
|
| 122 |
protected: |
| 123 |
CmdLineInterface *_parser; |
| 124 |
Container _args; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 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 |
|
| 166 |
|
| 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 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
class AnyOf : public ArgGroup { |
| 183 |
public: |
| 184 |
AnyOf() {} |
| 185 |
explicit AnyOf(CmdLineInterface &parser) { parser.add(*this); } |
| 186 |
|
| 187 |
bool validate() { return false; } |
| 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 |
|
| 216 |
|
| 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 = "{"; |
| 233 |
|
| 234 |
for (const_iterator it = begin(); it != end(); ++it) { |
| 235 |
name += sep + (*it)->getName(); |
| 236 |
sep = " | "; |
| 237 |
} |
| 238 |
|
| 239 |
return name + '}'; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 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 |
} |
| 255 |
|
| 256 |
#endif |