Changeset 10707


Ignore:
Timestamp:
May 13, 2011, 3:48:57 PM (8 years ago)
Author:
tbretz
Message:
Use the functionality of boost program_options better; implemented po_bool() as a special po_switch() case; added some comments.
Location:
trunk/FACT++/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Configuration.cc

    r10634 r10707  
    9292    po::options_description config("Section");
    9393    config.add_options()
    94         ("option1",    po_string(),              "This is option1")
    95         ("option2",    po_int(22),               "This is option2")
    96         ("option3,o",  po_double()->required(),  "This option is mandatory")
    97         ("option4",    po_int(&opt),             "This is option 4")
    98         ("option5",    po_strings(),             "A list of strings")
    99         ("option6",    po_strings(),             "A list of strings")
    100         ("option7",    po_strings(),             "A list of strings")
     94        ("option1",    var<string>(),                        "This is option1")
     95        ("option2",    var<int>(22),                         "This is option2")
     96        ("option3,o",  var<double>->required(),              "This option is mandatory")
     97        ("option4",    var<int>(&opt),                       "This is option 4")
     98        ("option5",    vars<string>(),                       "A list of strings")
     99        ("option6",    vars<string>(),                       "A list of strings")
     100        ("option7",    vars<string>,                         "A list of strings")
     101        ("option8",    var<string>()->implicit_value("val"), "Just a string")
     102        ("option9",    var<string>()->default_value("def"),  "Just a string")
     103        ("optionA",    var<string>("def"),                   "Just a string")
     104        ("bool",       po_bool(),                            "A special switch")
    101105        ;
    102106
     
    119123Abbreviations po_ints(), po_doubles() and po_strings() are available.
    120124
    121 In addition to options introduced by a minus, so calles positional options
    122 can be given on the command line. To describe these options use
     125There are several ways to define the behaviour of the options. In the
     126example above Parse will throw an exception if the "--option3" or "-o"
     127option is not given. "option9" will evaluate to "def" if it is not
     128given on the command line. The syntax of "optionA" is just an
     129abbreviation. "option8" will evaluate to "val" if just "--option5" but
     130no argument is given. Note, that these modifiers can be concatenated.
     131
     132A special type po_bool() is provided which is an abbreviation of
     133var<bool>()->implicit_value(true)->default_value(false). In
     134contradiction to po_switch() this allows to set a true and
     135false value in the setup file.
     136
     137In addition to options introduced by a minus or double minus, so called
     138positional options can be given on the command line. To describe these
     139options use
    123140
    124141\code
  • trunk/FACT++/src/Configuration.h

    r10372 r10707  
    143143{ return po::bool_switch(); }
    144144
     145inline po::typed_value<bool> *po_bool()
     146{ return po::value<bool>()->implicit_value(true)->default_value(false); }
     147
    145148#endif
  • trunk/FACT++/src/argv.cc

    r10372 r10707  
    8080//        (",A",          po_float(),                    "include path")
    8181        ("radec",         po::value<vector<double>>(),                    "include path")
     82        ("switch",        po_switch(),                    "include path")
     83        ("bool",          var<bool>()->implicit_value(true),                    "include path")
    8284        ;
    8385
     
    167169
    168170    cout << "Program " << argv[0] << " started successfully." << endl;
     171
     172    cout << conf.Has("switch") << " " << conf.Get<bool>("switch") << endl;
     173    cout << conf.Has("bool") << " " << conf.Get<bool>("bool") << endl;
     174
    169175    return 0;
    170176/*
  • trunk/FACT++/src/dataLogger.cc

    r10693 r10707  
    318318public:
    319319//      void setBlackWhiteList(const std::string& , bool);
    320         void Setup(Configuration& conf);
     320        bool SetConfiguration(Configuration& conf);
    321321
    322322private:
     
    681681       
    682682                //services parameters
    683                 fDebugIsOn = true;//false;
     683                fDebugIsOn = false;
    684684                fStatsPeriodDuration = 1.0f;
    685685                fOpenedFilesIsOn = true;
     
    12691269        Message("Run Path: " + actualTargetDir);
    12701270        stringstream str;
    1271         str << "Run Number: " << fRunFileName;
     1271        str << "Run Number: " << fRunNumber;
    12721272        Message(str.str());
    12731273        Message("----------- OPENED FILES ----------------");
     
    13871387                Message("Server "+it->first);
    13881388                for (std::map<std::string, SubscriptionType>::const_iterator it2=it->second.begin(); it2!=it->second.end(); it2++)
    1389                         Message("-> "+it2->first);
     1389                        Message(" -> "+it2->first);
    13901390        }
    13911391        if (fIsBlackList)
     
    13951395                        Message("----------- ALLOW LIST ------------------");
    13961396        for (set<string>::iterator it=fGreyList.begin(); it != fGreyList.end(); it++)
    1397                 Message(*it);
     1397            Message(*it);
     1398
     1399        if (fGreyList.size()==0)
     1400            Message(" <empty>");
     1401
    13981402        return GetCurrentState();
    13991403}
     
    20832087}
    20842088
    2085 void DataLogger::Setup(Configuration& conf)
    2086 {
     2089bool DataLogger::SetConfiguration(Configuration& conf)
     2090{
     2091    fDebugIsOn = conf.Get<bool>("debug");
     2092
    20872093        //Set the block or allow list
    20882094        fGreyList.clear();
     
    21132119        }
    21142120    }
     2121
     2122    return true;
    21152123}
    21162124/*
     
    21482156    // Start io_service.run to only use the commandHandler command detaching
    21492157    DataLogger logger(wout);
     2158    if (!logger.SetConfiguration(conf))
     2159        return -1;
     2160
    21502161    logger.Run(true);
    21512162
     
    21742185
    21752186    DataLogger logger(wout);
     2187    if (!logger.SetConfiguration(conf))
     2188        return -1;
    21762189   
    2177     logger.Setup(conf);
    2178 
    21792190    shell.SetReceiver(logger);
    21802191
     
    22562267    const string n = conf.GetName()+".log";
    22572268
    2258     po::options_description config("Program options");
    2259     config.add_options()
     2269    po::options_description configp("Program options");
     2270    configp.add_options()
    22602271        ("dns",       var<string>("localhost"),  "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
    22612272        ("log,l",     var<string>(n), "Write log-file")
    22622273        ("console,c", var<int>(),     "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
    2263         ("block,b", vars<string>(), "Black-list of services")
    2264         ("allow,a", vars<string>(), "White-list of services")
    22652274        ;
    22662275
     2276    po::options_description configs("Scheduler options");
     2277    configs.add_options()
     2278        ("block,b",   vars<string>(), "Black-list of services")
     2279        ("allow,a",   vars<string>(), "White-list of services")
     2280        ("debug",     po_bool(),      "Debug mode. Print clear text of received service reports to log-stream")
     2281        ;
     2282
    22672283    conf.AddEnv("dns", "DIM_DNS_NODE");
    22682284
    2269     conf.AddOptions(config);
     2285    conf.AddOptions(configp);
     2286    conf.AddOptions(configs);
    22702287}
    22712288
     
    23122329    Dim::Setup(conf.Get<string>("dns"));
    23132330
    2314     try
     2331//    try
    23152332    {
    23162333        // No console access at all
     
    23242341            return RunShell<LocalConsole>(conf);
    23252342    }
    2326     catch (std::exception& e)
     2343/*    catch (std::exception& e)
    23272344    {
    23282345        cerr << "Exception: " << e.what() << endl;
    23292346        return -1;
    2330     }
     2347    }*/
    23312348
    23322349    return 0;
  • trunk/FACT++/src/ftmctrl.cc

    r10700 r10707  
    17071707        ("dns",       var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
    17081708        ("log,l",     var<string>(n), "Write log-file")
    1709         ("no-dim,d",  po_switch(),    "Disable dim services")
     1709        ("no-dim,d",  po_bool(),      "Disable dim services")
    17101710        ("console,c", var<int>(),     "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
    17111711        ;
     
    17141714    control.add_options()
    17151715        ("addr,a",        var<string>("localhost:5000"),  "Network address of FTM")
    1716         ("quiet,q",       po_switch(),   "Disable printing contents of all received messages (except dynamic data) in clear text.")
    1717         ("hex-out",       po_switch(),   "Enable printing contents of all printed messages also as hex data.")
    1718         ("dynamic-out",   po_switch(),   "Enable printing received dynamic data.")
     1716        ("quiet,q",       po_bool(),  "Disable printing contents of all received messages (except dynamic data) in clear text.")
     1717        ("hex-out",       po_bool(),  "Enable printing contents of all printed messages also as hex data.")
     1718        ("dynamic-out",   po_bool(),  "Enable printing received dynamic data.")
    17191719//        ("default-setup", var<string>(), "Binary file with static data loaded whenever a connection to the FTM was established.")
    17201720        ;
  • trunk/FACT++/src/triggerschedule.cc

    r10683 r10707  
    1313    config.add_options()
    1414        ("dns",               var<string>("localhost"),  "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
    15         ("schedule-database", var<string>(), "Database name for scheduling preview")
     15        ("schedule-database", var<string>()->required(), "Database name for scheduling preview")
    1616        ;
    1717
     
    6464        vm = conf.Parse(argc, argv);
    6565    }
     66#if BOOST_VERSION > 104000
     67    catch (po::multiple_occurrences &e)
     68    {
     69        cout << "Error: " << e.what() << " of '" << e.get_option_name() << "' option." << endl;
     70        cout << endl;
     71        return -1;
     72    }
     73#endif
    6674    catch (std::exception &e)
    6775    {
    68 #if BOOST_VERSION > 104000
    69         po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
    70         if (MO)
    71             cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
    72         else
    73 #endif
    74             cout << "Error: " << e.what() << endl;
     76        cout << "Error: " << e.what() << endl;
    7577        cout << endl;
    7678
     
    9395    }
    9496
    95     if (!conf.Has("schedule-database"))
    96     {
    97         cout << "Please provide which database you want to use for scheduling using --schedule-database=<dbname>." << endl;
    98         return -1;
    99     }
    100 
    10197    const char* dbname = conf.Get<string>("schedule-database").c_str();
    10298
Note: See TracChangeset for help on using the changeset viewer.