source: trunk/Mars/mbase/MArgs.cc@ 19779

Last change on this file since 19779 was 9490, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 13.6 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 "";
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 "";
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 a pointer to the MArgsEntry of 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//
300MArgsEntry *MArgs::GetArgument(Int_t i) const
301{
302 Int_t num = 0;
303
304 Bool_t allarg = kFALSE;
305
306 TIter Next(&fArgv);
307 MArgsEntry *e = NULL;
308 while ((e=static_cast<MArgsEntry*>(Next())))
309 {
310 const TString &s=*dynamic_cast<TString*>(e);
311
312 if (s=="--")
313 {
314 allarg = kTRUE;
315 continue;
316 }
317
318 if (s.BeginsWith("-") && !allarg)
319 continue;
320
321 if (i==num++)
322 return e;
323 }
324
325 return 0;
326}
327
328// --------------------------------------------------------------------------
329//
330// Return the TString corresponding to the i-th argument.
331// This is ment for enumerations like
332// executable file1 file2 file3
333// GetArgumentStr(1) will return "file2"
334// Only arguments without a trailing '-' are considered
335//
336TString MArgs::GetArgumentStr(Int_t i) const
337{
338 const MArgsEntry *e = GetArgument(i);
339 return e==0 ? "" : dynamic_cast<const TString&>(*e);
340}
341
342// --------------------------------------------------------------------------
343//
344// return the number of arguments without a trainling '-'
345//
346Int_t MArgs::GetNumArguments() const
347{
348 Int_t num = 0;
349
350 Bool_t allarg = kFALSE;
351
352 TIter Next(&fArgv);
353 TString *s = NULL;
354 while ((s=dynamic_cast<TString*>(Next())))
355 {
356 if (*s=="--")
357 {
358 allarg = kTRUE;
359 continue;
360 }
361
362 if (s->BeginsWith("-") && !allarg)
363 continue;
364
365 num++;
366 }
367
368 return num;
369}
370
371// --------------------------------------------------------------------------
372//
373// Remove the i-th argument from the list. Return kTRUE in case of sucess
374// kFALSE otherwise
375//
376Bool_t MArgs::RemoveArgument(Int_t i)
377{
378 MArgsEntry *e = GetArgument(i);
379 if (!e)
380 return kFALSE;
381
382 delete fArgv.Remove(e);
383
384 return kTRUE;
385}
386
387// --------------------------------------------------------------------------
388//
389// return the number of arguments with a trainling '-'
390//
391Int_t MArgs::GetNumOptions() const
392{
393 Int_t num = 0;
394
395 TIter Next(&fArgv);
396 TString *s = NULL;
397 while ((s=dynamic_cast<TString*>(Next())))
398 {
399 if (*s=="--")
400 return num;
401
402 if (s->BeginsWith("-"))
403 num++;
404 }
405
406 return num;
407}
408
409// --------------------------------------------------------------------------
410//
411// return the total number of entries
412//
413Int_t MArgs::GetNumEntries() const
414{
415 return fArgv.FindObject("--") ? fArgv.GetSize()-1 : fArgv.GetSize();
416}
417
418// --------------------------------------------------------------------------
419//
420// Checks whether an argument beginning with 'n' is existing, eg:
421// executable -value5
422// executable -value
423// HasOption("-value") will return true in both cases
424//
425Bool_t MArgs::Has(const TString n) const
426{
427 const TString name = n.Strip(TString::kBoth);
428
429 TIter Next(&fArgv);
430 TString *s = NULL;
431 while ((s=dynamic_cast<TString*>(Next())))
432 if (s->BeginsWith(name))
433 return kTRUE;
434 return kFALSE;
435}
436
437// --------------------------------------------------------------------------
438//
439// Checks whether an argument beginning with 'n' is existing, eg:
440// executable -value5
441// HasOption("-value") will return false
442// executable -value
443// HasOption("-value") will return true
444//
445Bool_t MArgs::HasOnly(const TString n) const
446{
447 const TString name = n.Strip(TString::kBoth);
448
449 TIter Next(&fArgv);
450 TString *s = NULL;
451 while ((s=dynamic_cast<TString*>(Next())))
452 if (*s==name)
453 return kTRUE;
454 return kFALSE;
455}
456
457// --------------------------------------------------------------------------
458//
459// Checks whether an argument beginning with 'n' is exists and a
460// corresponding option is available, eg.
461// executable -value5
462// HasOption("-value") will return true
463// but:
464// executable -value
465// HasOption("-value") will return false
466//
467Bool_t MArgs::HasOption(const TString n) const
468{
469 const TString name = n.Strip(TString::kBoth);
470
471 TIter Next(&fArgv);
472 TString *s = NULL;
473 while ((s=dynamic_cast<TString*>(Next())))
474 if (s->BeginsWith(name) && s->Length()>name.Length())
475 return kTRUE;
476 return kFALSE;
477}
478
479// --------------------------------------------------------------------------
480//
481// Checks whether an argument beginning with 'n' is exists and a
482// corresponding option is available, eg.
483// executable -value5
484// HasOption("-value") will return false
485// but:
486// executable -value
487// HasOption("-value") will return true
488//
489// The argument is removed from the internal list.
490//
491Bool_t MArgs::HasOnlyAndRemove(const TString n)
492{
493 const TString name = n.Strip(TString::kBoth);
494
495 Bool_t rc = kFALSE;
496
497 TIter Next(&fArgv);
498 TString *s = NULL;
499 while ((s=dynamic_cast<TString*>(Next())))
500 if (*s==name)
501 {
502 delete fArgv.Remove(dynamic_cast<TObject*>(s));
503 rc = kTRUE;
504 }
505
506 return rc;
507}
508
509// --------------------------------------------------------------------------
510//
511// Return all arguments and options in the order as they are stored
512// in memory.
513//
514TString MArgs::GetCommandLine() const
515{
516 TString rc;
517
518 TIter Next(&fArgv);
519 TString *s = NULL;
520 while ((s=dynamic_cast<TString*>(Next())))
521 {
522 rc += *s;
523 rc += " ";
524 }
525
526 return rc.Strip(TString::kBoth);
527}
Note: See TracBrowser for help on using the repository browser.