Project
Loading...
Searching...
No Matches
OptionsHelpers.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#include "OptionsHelpers.h"
12#include "Framework/Logger.h"
13#include <boost/program_options.hpp>
14
15namespace bpo = boost::program_options;
16
17// Create a unique set of options and add them to the parser
18// This is needed to avoid duplicate definitions in the main parser
19// (e.g. if a device has a config option and a device-specific option)
20// However, we complain if there are duplicate definitions which have
21// different default values.
22// Notice it's probably a good idea to simply specify string options
23// and have the cast done in the user code. In any case what is
24// passes on the command line is a string.
25auto o2::framework::OptionsHelpers::makeUniqueOptions(bpo::options_description const& od) -> bpo::options_description
26{
27 bpo::options_description uniqueOptions;
28 std::set<std::string> uniqueNames;
29 std::map<std::string, std::string> optionDefaults;
30 for (auto& option : od.options()) {
31 if (uniqueNames.find(option->format_name()) == uniqueNames.end()) {
32 uniqueOptions.add(option);
33 uniqueNames.insert(option->format_name());
34 boost::any defaultValue;
35 option->semantic()->apply_default(defaultValue);
36 // check if defaultValue is a string and if so, store it
37 if (defaultValue.type() == typeid(std::string)) {
38 optionDefaults.insert({option->format_name(), boost::any_cast<std::string>(defaultValue)});
39 } else {
40 optionDefaults.insert({option->format_name(), "not a string"});
41 }
42 } else {
43 if (option->semantic()->max_tokens() == 1) {
44 LOG(debug) << "Option " << option->format_name() << " is already defined, skipping";
45 boost::any defaultValue1;
46 option->semantic()->apply_default(defaultValue1);
47 if (defaultValue1.type() != typeid(std::string)) {
48 LOGP(error, "Option {} is already defined but it's not a string, please fix it. Actualy type {}", option->format_name(), defaultValue1.type().name());
49 }
50 auto defaultValueStr1 = boost::any_cast<std::string>(defaultValue1);
51 auto defaultValueStr2 = optionDefaults.at(option->format_name());
52 if (defaultValueStr2 == "not a string") {
53 LOGP(error, "{} is duplicate but strings are the only supported duplicate values", option->format_name());
54 }
55 if (defaultValueStr1 != defaultValueStr2) {
56 LOGP(error, "Option {} has different default values: {} and {}", option->format_name(), defaultValueStr1, defaultValueStr2);
57 }
58 }
59 }
60 }
61 return uniqueOptions;
62};
std::ostringstream debug
static boost::program_options::options_description makeUniqueOptions(boost::program_options::options_description const &od)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"