Command Line Options Manager for ROOT

1.2

Introduction.

This class library supplies a number of classes for dealing with command line arguments in ROOT.

Using the classes in a program

The basic idea is simple

using namespace std;

int 
main(int argc, char** argv) 
{
  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);

One starts out defining the options that the application (or script) should have. The constructor of the classes TIntOption, TFloatOption, TStringOption, and TBoolOption takes 4 arguments:

  1. The short option identifier (single character). This is for options of the form -c where c is some character.
  2. The long option identifier (C character string). This is for options of the form --string where string is some character string.
  3. A string giving the help for the option
  4. The default value of the option.

  if (!TCommandLine::Instance()->ProcessCommandLine(argc, argv)) return 1;

Next, we ask the TCommandLine singleton to process the command line. If TCommandLine::ProcessCommandLine returns false, the parse failed.

  if (helpOption) TCommandLine::Instance()->Print();

If the option -h (or equivilant long option --help) was given, than we ask the manager to print a usage screen.

  cout << "The values given to the integer option are: " << endl;
  for (Int_t i = 0; i < intOption.GetNValues(); i++) 
    cout << "\t" << setw(3) << i << ": " << intOption[i] << endl;
  cout << "The values given to the real option are: " << endl;
  for (Int_t i = 0; i < floatOption.GetNValues(); i++) 
    cout << "\t" << setw(3) << i << ": " << floatOption[i] << endl;
  cout << "The values given to the string option are: " << endl;
  for (Int_t i = 0; i < stringOption.GetNValues(); i++) 
    cout << "\t" << setw(3) << i << ": " << stringOption[i] << endl;
    
  return 0;
}

Now we can get the values passed to the options. Here, we simply print them.

Using the classes in a script

One can also define options in a script and parse the command line from the script. All it requires is that the command line arguments have been registered with the command line manager using TCommandLine::SetCommandLine. For example, one could have the script
{
  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   quitOption('q',   "quit",   "ROOT commandline option", kFALSE);
  TBoolOption   helpOption('h',   "help",   "help",           kFALSE);

  if (!TCommandLine::Instance()->ProcessCommandLine()) 
    Fatal("test", "Parsing the command line failed");
  if (helpOption.Value()) TCommandLine::Instance()->Print();

  cout << "The values given to the integer option are: " << endl;
  for (Int_t i = 0; i < intOption.GetNValues(); i++) 
    cout << "\t" << setw(3) << i << ": " << intOption[i] << endl;
  cout << "The values given to the real option are: " << endl;
  for (Int_t i = 0; i < floatOption.GetNValues(); i++) 
    cout << "\t" << setw(3) << i << ": " << floatOption[i] << endl;
  cout << "The values given to the string option are: " << endl;
  for (Int_t i = 0; i < stringOption.GetNValues(); i++) 
    cout << "\t" << setw(3) << i << ": " << stringOption[i] << endl;
}

Again, we simply define the options, parse the command line and use the values.

This script could be envoked from a program like

using namespace std;

int 
main(int argc, char** argv) 
{
  TCommandLine::Instance()->SetCommandLine(argc, argv);
  gROOT->Macro("test.C");
  return 0;
}

Or we could invoke it from another script like

void 
testScript()
{
  gSystem->Load("libTOption.so"); 
  Int_t     argc = gApplication->Argc();
  Char_t**  argv = gApplication->Argv();
  TCommandLine::Instance()->SetCommandLine(argc, argv);
  gROOT->Macro("test.C");
}

Using the classes in configuration scripts

The first way to use the script, is very useful if you have some sort of configuration script you'd like to pass options to from the command line, and then the program it self executes some sort of loop. For example, one could have a script like
    { 
      TBoolOption* useTPC = new TIntOption('',"no-tpc",
                                              "Don't use the TPC",kTRUE);
      TStringOption* outOpt = new TStringOption('o',"output",
                                                "Output file", "data.root");
      ...
      if (!TCommandLine::ProcessCommandLine()) return;
      ... 
      Main* main = Main::Instance();
      if (useTPC.Value()) {
        TPC* tpc = new TPC;
        main->Add(tpc);
      }
      ...
      Output* output = new Output(outPut.Value());
      main->Add(output);
      ...
    }

and a program like

    int main(int argc, char** argv) 
    {
      TCommandLine::Instance()->SetCommandLine(argc, argv);
      gROOT->Macro("Config.C");
      
      Main* job = Main::Instance();
      return job->Loop();
    }
In this way, the configuration script can set up the job by inspecting the command line arguments given to the program (probably run in batch).

Note:
Such a setup was used by the BRAHMS collaboration for digitization, reconstruction, analysis, and user analysis. It proved to be very succesful and easy to deal with from both a maintainer and user point of view.
Top of page Last update Wed Dec 8 13:01:37 2004
Christian Holm
Created by DoxyGen 1.3.9.1