Re: [ROOT] getopt + root

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Tue Nov 12 2002 - 19:40:30 MET


Hi Martin, 

Martin Hennecke <hennecke@fnal.gov> wrote concerning
Re: [ROOT] getopt + root [Tue, 12 Nov 2002 10:15:21 -0600 (CST)]
----------------------------------------------------------------------
> 
> Hi Rene
> 
> Sure, one can do it that way but I think using getopt is more
> elegant ;-) 

As you are probably aware of, you're mixing apples and pears here. 

The interactive ROOT interpretor (root or root.exe) is the one that
gets the arguments from the command line - not your script.  

The global object `gApplication' of class `TRint', which is a
sub-class of `TApplication', however contains the command line
arguments, and you can access those via member functions
`TApplication::Argc' and `TApplication::Argv'.  You can use those
member functions to get the values, and do you're command line parsing
based on those if you like:  

  #!/usr/bin/root.exe -l -b -q foo.C 

  { 
    int argc    = gApplication->Argc();
    char** argv = gApplication->Argv();
    cout << "There was " << argc << "arguments on the command line" << endl;
    for (int i = 0; i < argc; i++) 
      cout << "Argument # " << i << ": " << argv[i] << endl;
    return 0;
  }

Incidentally, I once wrote a set of classes to handle command line
parameters in ROOT based applications.  You can find them at my
web-site [1].  With those classes, you could do something like: 

  #!/usr/bin/root.exe -l -b -q foo.C 

  { 
    TIntOption intOption('i',       "int",    "integer",        0);
    TFloatOption floatOption('f',   "float",  "floating point", .1);
    TStringOption stringOption('s', "string", "string",         "hello");
    TBoolOption boolOption('b',     "bool",   "boolean",        kTRUE);
    TBoolOption helpOption('h',     "help",   "help",           kFALSE);

    TCommandLine::Instance()->ProcessCommandLine(gApplication->Argc(), 
					         gApplication->Argv());
    if (helpOption) TCommandLine::Instance()->Print();
    
    for (Int_t i = 0; i < stringOption.GetNValues(); i++) { 
      cout << "The " << i << (i==1 ? "st" : 
                              (i==2 ? "nd" : 
			       (i==3 ? "rd" : "th")))
           << " value of string options is: " 
	   << stringOption->GetValue(i) << endl;
  }

The option classes should really be a template, but unfortunately I
wasn't really familiar with the template caveats of ROOT when I wrote
the classes, so ... :-( 

A word of warning:  Don't use interpretor lines for anything installed
outside of the regular Unix tree (/bin, /usr/bin, /usr/local/bin) -
it's not portable, and some systems may even disallow it.  Also, note
that `root' spawns a child process (`root.exe'), and `TApplication'
may choke on unknown command line options.   I'd really like to see
`TApplication' being a lot more flexible when it comes to command line
options.   Perhaps some sort of integration of the above mentioned
classes wouldn't be such a bad idea. 

Yours, 

 ___  |  Christian Holm Christensen 
  |_| |	 -------------------------------------------------------------
    | |	 Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|	          DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|	          Denmark                    Office: (+45) 353  25 305
 ____|	 Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
 | |

[1] http://cholm.home.cern.ch/cholm/root/#toption



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:17 MET