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 *o) 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 | MArgs::MArgs(int argc, char **argv) : fArgc(argc)
|
---|
60 | {
|
---|
61 | // FIXME: argv has no const-qualifier to be idetical with
|
---|
62 | // TApplication.
|
---|
63 | fName = argv[0];
|
---|
64 |
|
---|
65 | fArgv = new TList;
|
---|
66 | fArgv->SetOwner();
|
---|
67 |
|
---|
68 | for (int i=1; i<argc; i++)
|
---|
69 | {
|
---|
70 | MArgsEntry &o = *new MArgsEntry(argv[i]);
|
---|
71 | dynamic_cast<TString&>(o) = o.Strip(TString::kBoth);
|
---|
72 | fArgv->Add(&o);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | // --------------------------------------------------------------------------
|
---|
77 | //
|
---|
78 | // Deletes fArgv.
|
---|
79 | //
|
---|
80 | MArgs::~MArgs()
|
---|
81 | {
|
---|
82 | delete fArgv;
|
---|
83 | }
|
---|
84 |
|
---|
85 | // --------------------------------------------------------------------------
|
---|
86 | //
|
---|
87 | // Print everything parsed.
|
---|
88 | // Using 'options' as option only 'options' are printed.
|
---|
89 | // Using 'arguments' as option only 'arguments' are printed.
|
---|
90 | //
|
---|
91 | void MArgs::Print(const Option_t *o) const
|
---|
92 | {
|
---|
93 | gLog << all << underline << fName << ":" << endl;
|
---|
94 |
|
---|
95 | const TString str(o);
|
---|
96 |
|
---|
97 | if (!str.CompareTo("options", TString::kIgnoreCase))
|
---|
98 | {
|
---|
99 | TIter Next(fArgv);
|
---|
100 | TString *s = NULL;
|
---|
101 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
102 | if (s->BeginsWith("-"))
|
---|
103 | gLog << *s << endl;
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (!str.CompareTo("arguments", TString::kIgnoreCase))
|
---|
108 | {
|
---|
109 | TIter Next(fArgv);
|
---|
110 | TString *s = NULL;
|
---|
111 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
112 | if (!s->BeginsWith("-"))
|
---|
113 | gLog << *s << endl;
|
---|
114 | return;
|
---|
115 | }
|
---|
116 |
|
---|
117 | fArgv->Print();
|
---|
118 | }
|
---|
119 |
|
---|
120 | // --------------------------------------------------------------------------
|
---|
121 | //
|
---|
122 | // Return the Integer corresponding to the command line argument 'name'
|
---|
123 | // eg. executable -argument 5
|
---|
124 | // GetInt("argument") will return 5
|
---|
125 | //
|
---|
126 | Int_t MArgs::GetInt(const TString name) const
|
---|
127 | {
|
---|
128 | return atoi(GetString(name));
|
---|
129 | }
|
---|
130 |
|
---|
131 | // --------------------------------------------------------------------------
|
---|
132 | //
|
---|
133 | // Return the floating point value corresponding to the command line argument
|
---|
134 | // 'name'
|
---|
135 | // eg. executable -argument 5.7
|
---|
136 | // GetFloat("argument") will return 5.7
|
---|
137 | //
|
---|
138 | Double_t MArgs::GetFloat(const TString name) const
|
---|
139 | {
|
---|
140 | return atof(GetString(name));
|
---|
141 | }
|
---|
142 |
|
---|
143 | // --------------------------------------------------------------------------
|
---|
144 | //
|
---|
145 | // Return the TString corresponding to the command line argument 'name'
|
---|
146 | // eg. executable -argument=last
|
---|
147 | // GetString("-argument=") will return "last"
|
---|
148 | //
|
---|
149 | TString MArgs::GetString(const TString name) const
|
---|
150 | {
|
---|
151 | TIter Next(fArgv);
|
---|
152 | TString *s = NULL;
|
---|
153 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
154 | if (s->BeginsWith(name))
|
---|
155 | return s->Data()+s->Index(name)+name.Length();
|
---|
156 | return 0;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // --------------------------------------------------------------------------
|
---|
160 | //
|
---|
161 | // Return the Integer corresponding to the command line argument 'name'
|
---|
162 | // eg. executable -argument5
|
---|
163 | // GetIntAndRemove("-argument") will return 5
|
---|
164 | // and removes the argument from the internal list.
|
---|
165 | //
|
---|
166 | Int_t MArgs::GetIntAndRemove(const TString name)
|
---|
167 | {
|
---|
168 | return atoi(GetStringAndRemove(name));
|
---|
169 | }
|
---|
170 |
|
---|
171 | // --------------------------------------------------------------------------
|
---|
172 | //
|
---|
173 | // Return the floating point value corresponding to the command line argument
|
---|
174 | // 'name'
|
---|
175 | // eg. executable -argument5.7
|
---|
176 | // GetFloatAndRemove("-argument") will return 5.7
|
---|
177 | // and removes the argument from the internal list.
|
---|
178 | //
|
---|
179 | Double_t MArgs::GetFloatAndRemove(const TString name)
|
---|
180 | {
|
---|
181 | return atof(GetStringAndRemove(name));
|
---|
182 | }
|
---|
183 |
|
---|
184 | // --------------------------------------------------------------------------
|
---|
185 | //
|
---|
186 | // Return the TString corresponding to the command line argument 'name'
|
---|
187 | // eg. executable -argument=last
|
---|
188 | // GetStringAndRemove("-argument=") will return "last"
|
---|
189 | // and removes the argument from the internal list.
|
---|
190 | //
|
---|
191 | TString MArgs::GetStringAndRemove(const TString n)
|
---|
192 | {
|
---|
193 | const TString name = n.Strip(TString::kBoth);
|
---|
194 |
|
---|
195 | TIter Next(fArgv);
|
---|
196 | TString *s = NULL;
|
---|
197 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
198 | if (s->BeginsWith(name))
|
---|
199 | {
|
---|
200 | TString str = s->Data()+s->Index(name)+name.Length();
|
---|
201 | delete fArgv->Remove(dynamic_cast<TObject*>(s));
|
---|
202 | return str;
|
---|
203 | }
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | // --------------------------------------------------------------------------
|
---|
208 | //
|
---|
209 | // Return the Integer corresponding to the i-th argument. This is ment
|
---|
210 | // for enumerations like
|
---|
211 | // executable 1 7 2
|
---|
212 | // GetArgumentInt(1) will return 7
|
---|
213 | //
|
---|
214 | Int_t MArgs::GetArgumentInt(Int_t i) const
|
---|
215 | {
|
---|
216 | return atoi(GetArgumentStr(i));
|
---|
217 | }
|
---|
218 |
|
---|
219 | // --------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | // Return the floating point value corresponding to the i-th argument.
|
---|
222 | // This is ment for enumerations like
|
---|
223 | // executable 1.7 7.5 2.3
|
---|
224 | // GetArgumentFloat(1) will return 7.5
|
---|
225 | //
|
---|
226 | Float_t MArgs::GetArgumentFloat(Int_t i) const
|
---|
227 | {
|
---|
228 | return atof(GetArgumentStr(i));
|
---|
229 | }
|
---|
230 |
|
---|
231 | // --------------------------------------------------------------------------
|
---|
232 | //
|
---|
233 | // Returns GetIntAndRemove. If HasOption returns kFALSE def is returned.
|
---|
234 | //
|
---|
235 | Int_t MArgs::GetIntAndRemove(const TString name, Int_t def)
|
---|
236 | {
|
---|
237 | if (!HasOption(name))
|
---|
238 | return def;
|
---|
239 | return GetIntAndRemove(name);
|
---|
240 | }
|
---|
241 |
|
---|
242 | // --------------------------------------------------------------------------
|
---|
243 | //
|
---|
244 | // Returns GetFloatAndRemove. If HasOption returns kFALSE def is returned.
|
---|
245 | //
|
---|
246 | Double_t MArgs::GetFloatAndRemove(const TString name, Double_t def)
|
---|
247 | {
|
---|
248 | if (!HasOption(name))
|
---|
249 | return def;
|
---|
250 | return GetFloatAndRemove(name);
|
---|
251 | }
|
---|
252 |
|
---|
253 | // --------------------------------------------------------------------------
|
---|
254 | //
|
---|
255 | // Returns GetStringAndRemove. If HasOption returns kFALSE def is returned.
|
---|
256 | //
|
---|
257 | TString MArgs::GetStringAndRemove(const TString name, const TString def)
|
---|
258 | {
|
---|
259 | if (!HasOption(name))
|
---|
260 | return def;
|
---|
261 | return GetStringAndRemove(name);
|
---|
262 | }
|
---|
263 |
|
---|
264 | // --------------------------------------------------------------------------
|
---|
265 | //
|
---|
266 | // Return the TString corresponding to the i-th argument.
|
---|
267 | // This is ment for enumerations like
|
---|
268 | // executable file1 file2 file3
|
---|
269 | // GetArgumentStr(1) will return "file2"
|
---|
270 | // Only arguments without a trailing '-' are considered
|
---|
271 | //
|
---|
272 | TString MArgs::GetArgumentStr(Int_t i) const
|
---|
273 | {
|
---|
274 | Int_t num = 0;
|
---|
275 |
|
---|
276 | TIter Next(fArgv);
|
---|
277 | TString *s = NULL;
|
---|
278 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
279 | {
|
---|
280 | if (s->BeginsWith("-"))
|
---|
281 | continue;
|
---|
282 |
|
---|
283 | if (i==num++)
|
---|
284 | return *s;
|
---|
285 | }
|
---|
286 |
|
---|
287 | return "";
|
---|
288 | }
|
---|
289 |
|
---|
290 | // --------------------------------------------------------------------------
|
---|
291 | //
|
---|
292 | // return the number of arguments without a trainling '-'
|
---|
293 | //
|
---|
294 | Int_t MArgs::GetNumArguments() const
|
---|
295 | {
|
---|
296 | Int_t num = 0;
|
---|
297 |
|
---|
298 | TIter Next(fArgv);
|
---|
299 | TString *s = NULL;
|
---|
300 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
301 | if (!s->BeginsWith("-"))
|
---|
302 | num++;
|
---|
303 |
|
---|
304 | return num;
|
---|
305 | }
|
---|
306 |
|
---|
307 | // --------------------------------------------------------------------------
|
---|
308 | //
|
---|
309 | // return the number of arguments with a trainling '-'
|
---|
310 | //
|
---|
311 | Int_t MArgs::GetNumOptions() const
|
---|
312 | {
|
---|
313 | Int_t num = 0;
|
---|
314 |
|
---|
315 | TIter Next(fArgv);
|
---|
316 | TString *s = NULL;
|
---|
317 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
318 | if (s->BeginsWith("-"))
|
---|
319 | num++;
|
---|
320 |
|
---|
321 | return num;
|
---|
322 | }
|
---|
323 |
|
---|
324 | // --------------------------------------------------------------------------
|
---|
325 | //
|
---|
326 | // return the total number of entries
|
---|
327 | //
|
---|
328 | Int_t MArgs::GetNumEntries() const
|
---|
329 | {
|
---|
330 | return fArgv->GetSize();
|
---|
331 | }
|
---|
332 |
|
---|
333 | // --------------------------------------------------------------------------
|
---|
334 | //
|
---|
335 | // Checks whether an argument beginning with 'n' is existing, eg:
|
---|
336 | // executable -value5
|
---|
337 | // executable -value
|
---|
338 | // HasOption("-value") will return true in both cases
|
---|
339 | //
|
---|
340 | Bool_t MArgs::Has(const TString n) const
|
---|
341 | {
|
---|
342 | const TString name = n.Strip(TString::kBoth);
|
---|
343 |
|
---|
344 | TIter Next(fArgv);
|
---|
345 | TString *s = NULL;
|
---|
346 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
347 | if (s->BeginsWith(name))
|
---|
348 | return kTRUE;
|
---|
349 | return kFALSE;
|
---|
350 | }
|
---|
351 |
|
---|
352 | // --------------------------------------------------------------------------
|
---|
353 | //
|
---|
354 | // Checks whether an argument beginning with 'n' is existing, eg:
|
---|
355 | // executable -value5
|
---|
356 | // HasOption("-value") will return false
|
---|
357 | // executable -value
|
---|
358 | // HasOption("-value") will return true
|
---|
359 | //
|
---|
360 | Bool_t MArgs::HasOnly(const TString n) const
|
---|
361 | {
|
---|
362 | const TString name = n.Strip(TString::kBoth);
|
---|
363 |
|
---|
364 | TIter Next(fArgv);
|
---|
365 | TString *s = NULL;
|
---|
366 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
367 | if (*s==name)
|
---|
368 | return kTRUE;
|
---|
369 | return kFALSE;
|
---|
370 | }
|
---|
371 |
|
---|
372 | // --------------------------------------------------------------------------
|
---|
373 | //
|
---|
374 | // Checks whether an argument beginning with 'n' is exists and a
|
---|
375 | // corresponding option is available, eg.
|
---|
376 | // executable -value5
|
---|
377 | // HasOption("-value") will return true
|
---|
378 | // but:
|
---|
379 | // executable -value
|
---|
380 | // HasOption("-value") will return false
|
---|
381 | //
|
---|
382 | Bool_t MArgs::HasOption(const TString n) const
|
---|
383 | {
|
---|
384 | const TString name = n.Strip(TString::kBoth);
|
---|
385 |
|
---|
386 | TIter Next(fArgv);
|
---|
387 | TString *s = NULL;
|
---|
388 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
389 | if (s->BeginsWith(name) && s->Length()>name.Length())
|
---|
390 | return kTRUE;
|
---|
391 | return kFALSE;
|
---|
392 | }
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------------------
|
---|
395 | //
|
---|
396 | // Checks whether an argument beginning with 'n' is exists and a
|
---|
397 | // corresponding option is available, eg.
|
---|
398 | // executable -value5
|
---|
399 | // HasOption("-value") will return false
|
---|
400 | // but:
|
---|
401 | // executable -value
|
---|
402 | // HasOption("-value") will return true
|
---|
403 | //
|
---|
404 | // The argument is removed from the internal list.
|
---|
405 | //
|
---|
406 | Bool_t MArgs::HasOnlyAndRemove(const TString n)
|
---|
407 | {
|
---|
408 | const TString name = n.Strip(TString::kBoth);
|
---|
409 |
|
---|
410 | TIter Next(fArgv);
|
---|
411 | TString *s = NULL;
|
---|
412 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
413 | if (*s==name)
|
---|
414 | {
|
---|
415 | delete fArgv->Remove(dynamic_cast<TObject*>(s));
|
---|
416 | return kTRUE;
|
---|
417 | }
|
---|
418 |
|
---|
419 | return kFALSE;
|
---|
420 | }
|
---|