|
Command Line Options Manager for ROOT
1.2Introduction.This class library supplies a number of classes for dealing with command line arguments in ROOT.Using the classes in a programThe 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:
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
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 scriptOne 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 scriptsThe 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(); }
|