| 1 | /* ======================================================================== *\ | 
|---|
| 2 | ! | 
|---|
| 3 | ! * | 
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 8 | ! * | 
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its | 
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee, | 
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and | 
|---|
| 12 | ! * that both that copyright notice and this permission notice appear | 
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express | 
|---|
| 14 | ! * or implied warranty. | 
|---|
| 15 | ! * | 
|---|
| 16 | ! | 
|---|
| 17 | ! | 
|---|
| 18 | !   Author(s): Thomas Bretz, 7/2003 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2003-2004 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 27 | // | 
|---|
| 28 | // MArgs | 
|---|
| 29 | // | 
|---|
| 30 | // This is a helper class for executables to parse command line arguments | 
|---|
| 31 | // | 
|---|
| 32 | // Arguments beginning with a trailing '-' are called 'options'. | 
|---|
| 33 | // Arguments without a trailing '-' are considered 'arguments' | 
|---|
| 34 | // | 
|---|
| 35 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 36 | #include "MArgs.h" | 
|---|
| 37 |  | 
|---|
| 38 | #include <stdlib.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include "MLog.h" | 
|---|
| 41 | #include "MLogManip.h" | 
|---|
| 42 |  | 
|---|
| 43 | ClassImp(MArgsEntry); | 
|---|
| 44 | ClassImp(MArgs); | 
|---|
| 45 |  | 
|---|
| 46 | using namespace std; | 
|---|
| 47 |  | 
|---|
| 48 | void MArgsEntry::Print(const Option_t *) const | 
|---|
| 49 | { | 
|---|
| 50 | gLog << all << *this << endl; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | // -------------------------------------------------------------------------- | 
|---|
| 54 | // | 
|---|
| 55 | // Initializes: | 
|---|
| 56 | //  fName: The name of the executable | 
|---|
| 57 | //  fArgv: A TList containing all other command line arguments | 
|---|
| 58 | // | 
|---|
| 59 | //  If root==kFALSE all root commandline options are deleted from | 
|---|
| 60 | //  the list, namely: -b | 
|---|
| 61 | // | 
|---|
| 62 | MArgs::MArgs(int argc, char **argv, Bool_t root) : fArgc(argc) | 
|---|
| 63 | { | 
|---|
| 64 | // FIXME: argv has no const-qualifier to be idetical with | 
|---|
| 65 | //        TApplication. | 
|---|
| 66 | fName = argv[0]; | 
|---|
| 67 |  | 
|---|
| 68 | fArgv = new TList; | 
|---|
| 69 | fArgv->SetOwner(); | 
|---|
| 70 |  | 
|---|
| 71 | for (int i=1; i<argc; i++) | 
|---|
| 72 | { | 
|---|
| 73 | MArgsEntry &o = *new MArgsEntry(argv[i]); | 
|---|
| 74 | fArgv->Add(&o); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | if (root) | 
|---|
| 78 | return; | 
|---|
| 79 |  | 
|---|
| 80 | HasOnlyAndRemove("-b"); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | // -------------------------------------------------------------------------- | 
|---|
| 84 | // | 
|---|
| 85 | // Deletes fArgv. | 
|---|
| 86 | // | 
|---|
| 87 | MArgs::~MArgs() | 
|---|
| 88 | { | 
|---|
| 89 | delete fArgv; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | // -------------------------------------------------------------------------- | 
|---|
| 93 | // | 
|---|
| 94 | // Print everything parsed. | 
|---|
| 95 | // Using 'options' as option only 'options' are printed. | 
|---|
| 96 | // Using 'arguments' as option only 'arguments' are printed. | 
|---|
| 97 | // | 
|---|
| 98 | void MArgs::Print(const Option_t *o) const | 
|---|
| 99 | { | 
|---|
| 100 | gLog << all << underline << fName << ":" << endl; | 
|---|
| 101 |  | 
|---|
| 102 | const TString str(o); | 
|---|
| 103 |  | 
|---|
| 104 | if (!str.CompareTo("options", TString::kIgnoreCase)) | 
|---|
| 105 | { | 
|---|
| 106 | TIter Next(fArgv); | 
|---|
| 107 | TString *s = NULL; | 
|---|
| 108 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 109 | if (s->BeginsWith("-")) | 
|---|
| 110 | gLog << *s << endl; | 
|---|
| 111 | return; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | if (!str.CompareTo("arguments", TString::kIgnoreCase)) | 
|---|
| 115 | { | 
|---|
| 116 | TIter Next(fArgv); | 
|---|
| 117 | TString *s = NULL; | 
|---|
| 118 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 119 | if (!s->BeginsWith("-")) | 
|---|
| 120 | gLog << *s << endl; | 
|---|
| 121 | return; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | fArgv->Print(); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | // -------------------------------------------------------------------------- | 
|---|
| 128 | // | 
|---|
| 129 | // Return the Integer corresponding to the command line argument 'name' | 
|---|
| 130 | //  eg. executable -argument 5 | 
|---|
| 131 | //      GetInt("argument") will return 5 | 
|---|
| 132 | // | 
|---|
| 133 | Int_t MArgs::GetInt(const TString name) const | 
|---|
| 134 | { | 
|---|
| 135 | return atoi(GetString(name)); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | // -------------------------------------------------------------------------- | 
|---|
| 139 | // | 
|---|
| 140 | // Return the floating point value corresponding to the command line argument | 
|---|
| 141 | // 'name' | 
|---|
| 142 | //  eg. executable -argument 5.7 | 
|---|
| 143 | //      GetFloat("argument") will return 5.7 | 
|---|
| 144 | // | 
|---|
| 145 | Double_t MArgs::GetFloat(const TString name) const | 
|---|
| 146 | { | 
|---|
| 147 | return atof(GetString(name)); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | // -------------------------------------------------------------------------- | 
|---|
| 151 | // | 
|---|
| 152 | // Return the TString corresponding to the command line argument 'name' | 
|---|
| 153 | //  eg. executable -argument=last | 
|---|
| 154 | //      GetString("-argument=") will return "last" | 
|---|
| 155 | // | 
|---|
| 156 | TString MArgs::GetString(const TString name) const | 
|---|
| 157 | { | 
|---|
| 158 | TIter Next(fArgv); | 
|---|
| 159 | TString *s = NULL; | 
|---|
| 160 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 161 | if (s->BeginsWith(name)) | 
|---|
| 162 | return s->Data()+s->Index(name)+name.Length(); | 
|---|
| 163 | return 0; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | // -------------------------------------------------------------------------- | 
|---|
| 167 | // | 
|---|
| 168 | // Return the Integer corresponding to the command line argument 'name' | 
|---|
| 169 | //  eg. executable -argument5 | 
|---|
| 170 | //      GetIntAndRemove("-argument") will return 5 | 
|---|
| 171 | // and removes the argument from the internal list. | 
|---|
| 172 | // | 
|---|
| 173 | Int_t MArgs::GetIntAndRemove(const TString name) | 
|---|
| 174 | { | 
|---|
| 175 | return atoi(GetStringAndRemove(name)); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | // -------------------------------------------------------------------------- | 
|---|
| 179 | // | 
|---|
| 180 | // Return the floating point value corresponding to the command line argument | 
|---|
| 181 | // 'name' | 
|---|
| 182 | //  eg. executable -argument5.7 | 
|---|
| 183 | //      GetFloatAndRemove("-argument") will return 5.7 | 
|---|
| 184 | // and removes the argument from the internal list. | 
|---|
| 185 | // | 
|---|
| 186 | Double_t MArgs::GetFloatAndRemove(const TString name) | 
|---|
| 187 | { | 
|---|
| 188 | return atof(GetStringAndRemove(name)); | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | // -------------------------------------------------------------------------- | 
|---|
| 192 | // | 
|---|
| 193 | // Return the TString corresponding to the command line argument 'name' | 
|---|
| 194 | //  eg. executable -argument=last | 
|---|
| 195 | //      GetStringAndRemove("-argument=") will return "last" | 
|---|
| 196 | // and removes the argument from the internal list. | 
|---|
| 197 | // | 
|---|
| 198 | TString MArgs::GetStringAndRemove(const TString n) | 
|---|
| 199 | { | 
|---|
| 200 | const TString name = n.Strip(TString::kBoth); | 
|---|
| 201 |  | 
|---|
| 202 | TIter Next(fArgv); | 
|---|
| 203 | TString *s = NULL; | 
|---|
| 204 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 205 | if (s->BeginsWith(name)) | 
|---|
| 206 | { | 
|---|
| 207 | TString str = s->Data()+s->Index(name)+name.Length(); | 
|---|
| 208 | delete fArgv->Remove(dynamic_cast<TObject*>(s)); | 
|---|
| 209 | return str; | 
|---|
| 210 | } | 
|---|
| 211 | return 0; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | // -------------------------------------------------------------------------- | 
|---|
| 215 | // | 
|---|
| 216 | // Return the Integer corresponding to the i-th argument. This is ment | 
|---|
| 217 | // for enumerations like | 
|---|
| 218 | //  executable 1 7 2 | 
|---|
| 219 | //  GetArgumentInt(1) will return 7 | 
|---|
| 220 | // | 
|---|
| 221 | Int_t MArgs::GetArgumentInt(Int_t i) const | 
|---|
| 222 | { | 
|---|
| 223 | return atoi(GetArgumentStr(i)); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | // -------------------------------------------------------------------------- | 
|---|
| 227 | // | 
|---|
| 228 | // Return the floating point value corresponding to the i-th argument. | 
|---|
| 229 | // This is ment for enumerations like | 
|---|
| 230 | //  executable 1.7 7.5 2.3 | 
|---|
| 231 | //  GetArgumentFloat(1) will return 7.5 | 
|---|
| 232 | // | 
|---|
| 233 | Float_t MArgs::GetArgumentFloat(Int_t i) const | 
|---|
| 234 | { | 
|---|
| 235 | return atof(GetArgumentStr(i)); | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | // -------------------------------------------------------------------------- | 
|---|
| 239 | // | 
|---|
| 240 | // Returns GetIntAndRemove. If HasOption returns kFALSE def is returned. | 
|---|
| 241 | // | 
|---|
| 242 | Int_t MArgs::GetIntAndRemove(const TString name, Int_t def) | 
|---|
| 243 | { | 
|---|
| 244 | if (!HasOption(name)) | 
|---|
| 245 | return def; | 
|---|
| 246 | return GetIntAndRemove(name); | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | // -------------------------------------------------------------------------- | 
|---|
| 250 | // | 
|---|
| 251 | // Returns GetFloatAndRemove. If HasOption returns kFALSE def is returned. | 
|---|
| 252 | // | 
|---|
| 253 | Double_t MArgs::GetFloatAndRemove(const TString name, Double_t def) | 
|---|
| 254 | { | 
|---|
| 255 | if (!HasOption(name)) | 
|---|
| 256 | return def; | 
|---|
| 257 | return GetFloatAndRemove(name); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | // -------------------------------------------------------------------------- | 
|---|
| 261 | // | 
|---|
| 262 | // Returns GetStringAndRemove. If HasOption returns kFALSE def is returned. | 
|---|
| 263 | // | 
|---|
| 264 | TString MArgs::GetStringAndRemove(const TString name, const TString def) | 
|---|
| 265 | { | 
|---|
| 266 | if (!HasOption(name)) | 
|---|
| 267 | return def; | 
|---|
| 268 | return GetStringAndRemove(name); | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | // -------------------------------------------------------------------------- | 
|---|
| 272 | // | 
|---|
| 273 | // Return the TString corresponding to the i-th argument. | 
|---|
| 274 | // This is ment for enumerations like | 
|---|
| 275 | //  executable file1 file2 file3 | 
|---|
| 276 | //  GetArgumentStr(1) will return "file2" | 
|---|
| 277 | // Only arguments without a trailing '-' are considered | 
|---|
| 278 | // | 
|---|
| 279 | TString MArgs::GetArgumentStr(Int_t i) const | 
|---|
| 280 | { | 
|---|
| 281 | Int_t num = 0; | 
|---|
| 282 |  | 
|---|
| 283 | TIter Next(fArgv); | 
|---|
| 284 | TString *s = NULL; | 
|---|
| 285 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 286 | { | 
|---|
| 287 | if (s->BeginsWith("-")) | 
|---|
| 288 | continue; | 
|---|
| 289 |  | 
|---|
| 290 | if (i==num++) | 
|---|
| 291 | return *s; | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | return ""; | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | // -------------------------------------------------------------------------- | 
|---|
| 298 | // | 
|---|
| 299 | // return the number of arguments without a trainling '-' | 
|---|
| 300 | // | 
|---|
| 301 | Int_t MArgs::GetNumArguments() const | 
|---|
| 302 | { | 
|---|
| 303 | Int_t num = 0; | 
|---|
| 304 |  | 
|---|
| 305 | TIter Next(fArgv); | 
|---|
| 306 | TString *s = NULL; | 
|---|
| 307 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 308 | if (!s->BeginsWith("-")) | 
|---|
| 309 | num++; | 
|---|
| 310 |  | 
|---|
| 311 | return num; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | // -------------------------------------------------------------------------- | 
|---|
| 315 | // | 
|---|
| 316 | // return the number of arguments with a trainling '-' | 
|---|
| 317 | // | 
|---|
| 318 | Int_t MArgs::GetNumOptions() const | 
|---|
| 319 | { | 
|---|
| 320 | Int_t num = 0; | 
|---|
| 321 |  | 
|---|
| 322 | TIter Next(fArgv); | 
|---|
| 323 | TString *s = NULL; | 
|---|
| 324 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 325 | if (s->BeginsWith("-")) | 
|---|
| 326 | num++; | 
|---|
| 327 |  | 
|---|
| 328 | return num; | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | // -------------------------------------------------------------------------- | 
|---|
| 332 | // | 
|---|
| 333 | // return the total number of entries | 
|---|
| 334 | // | 
|---|
| 335 | Int_t MArgs::GetNumEntries() const | 
|---|
| 336 | { | 
|---|
| 337 | return fArgv->GetSize(); | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | // -------------------------------------------------------------------------- | 
|---|
| 341 | // | 
|---|
| 342 | // Checks whether an argument beginning with 'n' is existing, eg: | 
|---|
| 343 | //  executable -value5 | 
|---|
| 344 | //  executable -value | 
|---|
| 345 | //  HasOption("-value") will return true in both cases | 
|---|
| 346 | // | 
|---|
| 347 | Bool_t MArgs::Has(const TString n) const | 
|---|
| 348 | { | 
|---|
| 349 | const TString name = n.Strip(TString::kBoth); | 
|---|
| 350 |  | 
|---|
| 351 | TIter Next(fArgv); | 
|---|
| 352 | TString *s = NULL; | 
|---|
| 353 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 354 | if (s->BeginsWith(name)) | 
|---|
| 355 | return kTRUE; | 
|---|
| 356 | return kFALSE; | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | // -------------------------------------------------------------------------- | 
|---|
| 360 | // | 
|---|
| 361 | // Checks whether an argument beginning with 'n' is existing, eg: | 
|---|
| 362 | //  executable -value5 | 
|---|
| 363 | //   HasOption("-value") will return false | 
|---|
| 364 | //  executable -value | 
|---|
| 365 | //   HasOption("-value") will return true | 
|---|
| 366 | // | 
|---|
| 367 | Bool_t MArgs::HasOnly(const TString n) const | 
|---|
| 368 | { | 
|---|
| 369 | const TString name = n.Strip(TString::kBoth); | 
|---|
| 370 |  | 
|---|
| 371 | TIter Next(fArgv); | 
|---|
| 372 | TString *s = NULL; | 
|---|
| 373 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 374 | if (*s==name) | 
|---|
| 375 | return kTRUE; | 
|---|
| 376 | return kFALSE; | 
|---|
| 377 | } | 
|---|
| 378 |  | 
|---|
| 379 | // -------------------------------------------------------------------------- | 
|---|
| 380 | // | 
|---|
| 381 | // Checks whether an argument beginning with 'n' is exists and a | 
|---|
| 382 | // corresponding option is available, eg. | 
|---|
| 383 | //  executable -value5 | 
|---|
| 384 | //  HasOption("-value") will return true | 
|---|
| 385 | // but: | 
|---|
| 386 | //  executable -value | 
|---|
| 387 | //  HasOption("-value") will return false | 
|---|
| 388 | // | 
|---|
| 389 | Bool_t MArgs::HasOption(const TString n) const | 
|---|
| 390 | { | 
|---|
| 391 | const TString name = n.Strip(TString::kBoth); | 
|---|
| 392 |  | 
|---|
| 393 | TIter Next(fArgv); | 
|---|
| 394 | TString *s = NULL; | 
|---|
| 395 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 396 | if (s->BeginsWith(name) && s->Length()>name.Length()) | 
|---|
| 397 | return kTRUE; | 
|---|
| 398 | return kFALSE; | 
|---|
| 399 | } | 
|---|
| 400 |  | 
|---|
| 401 | // -------------------------------------------------------------------------- | 
|---|
| 402 | // | 
|---|
| 403 | // Checks whether an argument beginning with 'n' is exists and a | 
|---|
| 404 | // corresponding option is available, eg. | 
|---|
| 405 | //  executable -value5 | 
|---|
| 406 | //  HasOption("-value") will return false | 
|---|
| 407 | // but: | 
|---|
| 408 | //  executable -value | 
|---|
| 409 | //  HasOption("-value") will return true | 
|---|
| 410 | // | 
|---|
| 411 | // The argument is removed from the internal list. | 
|---|
| 412 | // | 
|---|
| 413 | Bool_t MArgs::HasOnlyAndRemove(const TString n) | 
|---|
| 414 | { | 
|---|
| 415 | const TString name = n.Strip(TString::kBoth); | 
|---|
| 416 |  | 
|---|
| 417 | Bool_t rc = kFALSE; | 
|---|
| 418 |  | 
|---|
| 419 | TIter Next(fArgv); | 
|---|
| 420 | TString *s = NULL; | 
|---|
| 421 | while ((s=dynamic_cast<TString*>(Next()))) | 
|---|
| 422 | if (*s==name) | 
|---|
| 423 | { | 
|---|
| 424 | delete fArgv->Remove(dynamic_cast<TObject*>(s)); | 
|---|
| 425 | rc = kTRUE; | 
|---|
| 426 | } | 
|---|
| 427 |  | 
|---|
| 428 | return rc; | 
|---|
| 429 | } | 
|---|