source: trunk/MagicSoft/Mars/mbase/MArgs.cc@ 9204

Last change on this file since 9204 was 9141, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 12.3 KB
Line 
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-2008
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//
34// Arguments without a trailing '-' are considered 'arguments'
35//
36// All arguments appearing after '--' on the commandline are
37// also cosidered as 'arguments'
38//
39//////////////////////////////////////////////////////////////////////////////
40#include "MArgs.h"
41
42#include <stdlib.h>
43
44#include "MLog.h"
45#include "MLogManip.h"
46
47ClassImp(MArgsEntry);
48ClassImp(MArgs);
49
50using namespace std;
51
52void MArgsEntry::Print(const Option_t *) const
53{
54 gLog << all << *this << endl;
55}
56
57// --------------------------------------------------------------------------
58//
59// Initializes:
60// fName: The name of the executable
61// fArgv: A TList containing all other command line arguments
62//
63MArgs::MArgs(int argc, char **argv) : fArgc(argc)
64{
65 TString cmdline;
66 // FIXME: argv has no const-qualifier to be idetical with
67 // TApplication.
68 fName = argv[0];
69 fTitle = argv[0];
70
71 fArgv.SetOwner();
72
73 for (int i=1; i<argc; i++)
74 {
75 fTitle += " ";
76 fTitle += argv[i];
77 fArgv.Add(new MArgsEntry(argv[i]));
78 }
79}
80
81// --------------------------------------------------------------------------
82//
83// Remove all root commandline options from the list,
84// namely: -b, -n, -q, -l, -?, -h, --help, -config
85//
86// Returns the number of found root options (max 8)
87//
88Int_t MArgs::RemoveRootArgs()
89{
90 Int_t n = 0;
91
92 n += HasOnlyAndRemove("-b");
93 n += HasOnlyAndRemove("-n");
94 n += HasOnlyAndRemove("-q");
95 n += HasOnlyAndRemove("-l");
96 n += HasOnlyAndRemove("-?");
97 n += HasOnlyAndRemove("-h");
98 n += HasOnlyAndRemove("--help");
99 n += HasOnlyAndRemove("-config");
100
101 return n;
102}
103
104// --------------------------------------------------------------------------
105//
106// Print everything parsed.
107// Using 'options' as option only 'options' are printed.
108// Using 'arguments' as option only 'arguments' are printed.
109// Using 'cmdline' as option the command line is printed instead of
110// just the program name
111//
112void MArgs::Print(const Option_t *o) const
113{
114 TString str(o);
115
116 gLog << all << underline;
117 if (str.Contains("cmdline"))
118 gLog << fTitle << endl;
119 else
120 gLog << fName << ":" << endl;
121
122 str.ReplaceAll("cmdline", "");
123 str.ReplaceAll(" ", "");
124
125 if (!str.CompareTo("options", TString::kIgnoreCase))
126 {
127 TIter Next(&fArgv);
128 TString *s = NULL;
129 while ((s=dynamic_cast<TString*>(Next())))
130 if (*s!="--" && s->BeginsWith("-"))
131 gLog << *s << endl;
132 return;
133 }
134
135 if (!str.CompareTo("arguments", TString::kIgnoreCase))
136 {
137 TIter Next(&fArgv);
138 TString *s = NULL;
139 while ((s=dynamic_cast<TString*>(Next())))
140 if (*s!="--" && !s->BeginsWith("-"))
141 gLog << *s << endl;
142 return;
143 }
144
145 fArgv.Print();
146}
147
148// --------------------------------------------------------------------------
149//
150// Return the Integer corresponding to the command line argument 'name'
151// eg. executable -argument 5
152// GetInt("argument") will return 5
153//
154Int_t MArgs::GetInt(const TString name) const
155{
156 return atoi(GetString(name));
157}
158
159// --------------------------------------------------------------------------
160//
161// Return the floating point value corresponding to the command line argument
162// 'name'
163// eg. executable -argument 5.7
164// GetFloat("argument") will return 5.7
165//
166Double_t MArgs::GetFloat(const TString name) const
167{
168 return atof(GetString(name));
169}
170
171// --------------------------------------------------------------------------
172//
173// Return the TString corresponding to the command line argument 'name'
174// eg. executable -argument=last
175// GetString("-argument=") will return "last"
176//
177TString MArgs::GetString(const TString name) const
178{
179 TIter Next(&fArgv);
180 TString *s = NULL;
181 while ((s=dynamic_cast<TString*>(Next())))
182 if (s->BeginsWith(name))
183 return s->Data()+s->Index(name)+name.Length();
184 return 0;
185}
186
187// --------------------------------------------------------------------------
188//
189// Return the Integer corresponding to the command line argument 'name'
190// eg. executable -argument5
191// GetIntAndRemove("-argument") will return 5
192// and removes the argument from the internal list.
193//
194Int_t MArgs::GetIntAndRemove(const TString name)
195{
196 return atoi(GetStringAndRemove(name));
197}
198
199// --------------------------------------------------------------------------
200//
201// Return the floating point value corresponding to the command line argument
202// 'name'
203// eg. executable -argument5.7
204// GetFloatAndRemove("-argument") will return 5.7
205// and removes the argument from the internal list.
206//
207Double_t MArgs::GetFloatAndRemove(const TString name)
208{
209 return atof(GetStringAndRemove(name));
210}
211
212// --------------------------------------------------------------------------
213//
214// Return the TString corresponding to the command line argument 'name'
215// eg. executable -argument=last
216// GetStringAndRemove("-argument=") will return "last"
217// and removes the argument from the internal list.
218//
219TString MArgs::GetStringAndRemove(const TString n)
220{
221 const TString name = n.Strip(TString::kBoth);
222
223 TIter Next(&fArgv);
224 TString *s = NULL;
225 while ((s=dynamic_cast<TString*>(Next())))
226 if (s->BeginsWith(name))
227 {
228 TString str = s->Data()+s->Index(name)+name.Length();
229 delete fArgv.Remove(dynamic_cast<TObject*>(s));
230 return str;
231 }
232 return 0;
233}
234
235// --------------------------------------------------------------------------
236//
237// Return the Integer corresponding to the i-th argument. This is ment
238// for enumerations like
239// executable 1 7 2
240// GetArgumentInt(1) will return 7
241//
242Int_t MArgs::GetArgumentInt(Int_t i) const
243{
244 return atoi(GetArgumentStr(i));
245}
246
247// --------------------------------------------------------------------------
248//
249// Return the floating point value corresponding to the i-th argument.
250// This is ment for enumerations like
251// executable 1.7 7.5 2.3
252// GetArgumentFloat(1) will return 7.5
253//
254Float_t MArgs::GetArgumentFloat(Int_t i) const
255{
256 return atof(GetArgumentStr(i));
257}
258
259// --------------------------------------------------------------------------
260//
261// Returns GetIntAndRemove. If HasOption returns kFALSE def is returned.
262//
263Int_t MArgs::GetIntAndRemove(const TString name, Int_t def)
264{
265 if (!HasOption(name))
266 return def;
267 return GetIntAndRemove(name);
268}
269
270// --------------------------------------------------------------------------
271//
272// Returns GetFloatAndRemove. If HasOption returns kFALSE def is returned.
273//
274Double_t MArgs::GetFloatAndRemove(const TString name, Double_t def)
275{
276 if (!HasOption(name))
277 return def;
278 return GetFloatAndRemove(name);
279}
280
281// --------------------------------------------------------------------------
282//
283// Returns GetStringAndRemove. If HasOption returns kFALSE def is returned.
284//
285TString MArgs::GetStringAndRemove(const TString name, const TString def)
286{
287 if (!HasOption(name))
288 return def;
289 return GetStringAndRemove(name);
290}
291
292// --------------------------------------------------------------------------
293//
294// Return the TString corresponding to the i-th argument.
295// This is ment for enumerations like
296// executable file1 file2 file3
297// GetArgumentStr(1) will return "file2"
298// Only arguments without a trailing '-' are considered
299//
300TString MArgs::GetArgumentStr(Int_t i) const
301{
302 Int_t num = 0;
303
304 Bool_t allarg = kFALSE;
305
306 TIter Next(&fArgv);
307 TString *s = NULL;
308 while ((s=dynamic_cast<TString*>(Next())))
309 {
310 if (*s=="--")
311 {
312 allarg = kTRUE;
313 continue;
314 }
315
316 if (s->BeginsWith("-") && !allarg)
317 continue;
318
319 if (i==num++)
320 return *s;
321 }
322
323 return "";
324}
325
326// --------------------------------------------------------------------------
327//
328// return the number of arguments without a trainling '-'
329//
330Int_t MArgs::GetNumArguments() const
331{
332 Int_t num = 0;
333
334 Bool_t allarg = kFALSE;
335
336 TIter Next(&fArgv);
337 TString *s = NULL;
338 while ((s=dynamic_cast<TString*>(Next())))
339 {
340 if (*s=="--")
341 {
342 allarg = kTRUE;
343 continue;
344 }
345
346 if (s->BeginsWith("-") && !allarg)
347 continue;
348
349 num++;
350 }
351
352 return num;
353}
354
355// --------------------------------------------------------------------------
356//
357// return the number of arguments with a trainling '-'
358//
359Int_t MArgs::GetNumOptions() const
360{
361 Int_t num = 0;
362
363 TIter Next(&fArgv);
364 TString *s = NULL;
365 while ((s=dynamic_cast<TString*>(Next())))
366 {
367 if (*s=="--")
368 return num;
369
370 if (s->BeginsWith("-"))
371 num++;
372 }
373
374 return num;
375}
376
377// --------------------------------------------------------------------------
378//
379// return the total number of entries
380//
381Int_t MArgs::GetNumEntries() const
382{
383 return fArgv.FindObject("--") ? fArgv.GetSize()-1 : fArgv.GetSize();
384}
385
386// --------------------------------------------------------------------------
387//
388// Checks whether an argument beginning with 'n' is existing, eg:
389// executable -value5
390// executable -value
391// HasOption("-value") will return true in both cases
392//
393Bool_t MArgs::Has(const TString n) const
394{
395 const TString name = n.Strip(TString::kBoth);
396
397 TIter Next(&fArgv);
398 TString *s = NULL;
399 while ((s=dynamic_cast<TString*>(Next())))
400 if (s->BeginsWith(name))
401 return kTRUE;
402 return kFALSE;
403}
404
405// --------------------------------------------------------------------------
406//
407// Checks whether an argument beginning with 'n' is existing, eg:
408// executable -value5
409// HasOption("-value") will return false
410// executable -value
411// HasOption("-value") will return true
412//
413Bool_t MArgs::HasOnly(const TString n) const
414{
415 const TString name = n.Strip(TString::kBoth);
416
417 TIter Next(&fArgv);
418 TString *s = NULL;
419 while ((s=dynamic_cast<TString*>(Next())))
420 if (*s==name)
421 return kTRUE;
422 return kFALSE;
423}
424
425// --------------------------------------------------------------------------
426//
427// Checks whether an argument beginning with 'n' is exists and a
428// corresponding option is available, eg.
429// executable -value5
430// HasOption("-value") will return true
431// but:
432// executable -value
433// HasOption("-value") will return false
434//
435Bool_t MArgs::HasOption(const TString n) const
436{
437 const TString name = n.Strip(TString::kBoth);
438
439 TIter Next(&fArgv);
440 TString *s = NULL;
441 while ((s=dynamic_cast<TString*>(Next())))
442 if (s->BeginsWith(name) && s->Length()>name.Length())
443 return kTRUE;
444 return kFALSE;
445}
446
447// --------------------------------------------------------------------------
448//
449// Checks whether an argument beginning with 'n' is exists and a
450// corresponding option is available, eg.
451// executable -value5
452// HasOption("-value") will return false
453// but:
454// executable -value
455// HasOption("-value") will return true
456//
457// The argument is removed from the internal list.
458//
459Bool_t MArgs::HasOnlyAndRemove(const TString n)
460{
461 const TString name = n.Strip(TString::kBoth);
462
463 Bool_t rc = kFALSE;
464
465 TIter Next(&fArgv);
466 TString *s = NULL;
467 while ((s=dynamic_cast<TString*>(Next())))
468 if (*s==name)
469 {
470 delete fArgv.Remove(dynamic_cast<TObject*>(s));
471 rc = kTRUE;
472 }
473
474 return rc;
475}
Note: See TracBrowser for help on using the repository browser.