#include #include #include #include #include #include #include #include #include #include #include #include ClassImp(TAppOptionManager); // Options class TAppOptionManager::TAppOptionManager(Int_t major, Int_t minor, const Char_t* versionString, const Char_t* helpString) : fMajorVersion(major), fMinorVersion(minor) { // Constructor. // First two arguments are the major and version of this // application. third is the version string, and fourth is the help // string. // Set version string if (!versionString) fVersionString = new TString(""); else fVersionString = new TString(versionString); // Set help string if (!helpString) fHelpString = new TString("\n"); else fHelpString = new TString(helpString); // initialize internal table fTable = new THashList(10); // Set the two default otptions fHelpOption = new TAppBoolOption('h', "help", "Show this help",kFALSE); fVersionOption = new TAppBoolOption('V', "version", "Version number",kFALSE); AddOption(fHelpOption); AddOption(fVersionOption); } void TAppOptionManager::AddOption(TAppOption* option) { // Add an option to the manager fTable->Add(option); } TAppOption* TAppOptionManager::GetOption(Char_t option) { // Get the TAppOption with short name 'option' TIter next(fTable); TAppOption* opt; while ((opt = (TAppOption*)next())) { if (opt->GetShortName() == option) return opt; } return 0; } TAppOption* TAppOptionManager::GetOption(Char_t* option) { // Get the TAppOption with long name "option" // TIter next(fTable); // TAppOption* opt; // while ((opt = (TAppOption*)next())) { // if (strcmp(opt->GetLongName(),option) == 0) // return opt; // } return (TAppOption*)fTable->FindObject(option); } void TAppOptionManager::SetOptionValue(Char_t option, Char_t* value) { // Set the value of the option with short name 'option' TAppOption* opt = GetOption(option); if (!opt) throw "No such option"; opt->SetValue(value); } void TAppOptionManager::SetOptionValue(Char_t* option, Char_t* value) { // Set the value of the option with long name "option" TAppOption* opt = GetOption(option); if (!opt) throw "No such option"; opt->SetValue(value); } void TAppOptionManager::ProcessCommandLine(Int_t& argc, Char_t** argv) { // Process command line parameters fProgName = new TString(gSystem->BaseName(argv[0])); // Sort hash list fTable->Sort(); for (Int_t i = 1; i < argc; i++) { // See if we got an option if (argv[i][0] == '-') { // See if we got a long option Bool_t killNext = kFALSE; if (!argv[i][1] || argv[i][1] == '\0') continue; if (argv[i][1] == '-') killNext = ProcessLongOption(argv[i]+2); else { Int_t len = strlen(argv[i]); for (Int_t j = 1; j < len; j++) { // if we got a short option, get the option killNext = ProcessShortOption(argv[i][j],argv[i+1]); if (killNext) { if (argv[i][j+1] != '\0') throw "Option need an argument"; break; } } } // Set the command line argument to zero, so that it's ignored // hereafter argv[i] = 0; if (killNext) argv[++i] = 0; } } Int_t j = 0; Int_t i = 0; for (i = 0; i < argc; i++) { if (argv[i]) argv[j++] = argv[i]; } argc = j; } Bool_t TAppOptionManager::ProcessLongOption(Char_t* argument) { // Deal with long options of the form "--