Project
Loading...
Searching...
No Matches
ConfigParamsHelper.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.
13#include <boost/program_options.hpp>
14
15#include <string>
16#include <vector>
17#include <iostream>
18
19namespace bpo = boost::program_options;
20
21namespace o2::framework
22{
23
27 bpo::options_description& options,
28 const std::vector<ConfigParamSpec>& specs,
29 bpo::options_description vetos)
30{
31 auto proxy = options.add_options();
32 for (auto const& spec : specs) {
33 // skip everything found in the veto definition
34 if (vetos.find_nothrow(spec.name, false) != nullptr) {
35 continue;
36 }
37
38 switch (spec.type) {
39 // FIXME: Should we handle int and size_t diffently?
40 // FIXME: We should probably raise an error if the type is unknown
42 addConfigSpecOption<VariantType::Int>(spec, options);
43 break;
45 addConfigSpecOption<VariantType::Int8>(spec, options);
46 break;
48 addConfigSpecOption<VariantType::Int16>(spec, options);
49 break;
51 addConfigSpecOption<VariantType::Int64>(spec, options);
52 break;
54 addConfigSpecOption<VariantType::UInt8>(spec, options);
55 break;
57 addConfigSpecOption<VariantType::UInt16>(spec, options);
58 break;
60 addConfigSpecOption<VariantType::UInt32>(spec, options);
61 break;
63 addConfigSpecOption<VariantType::UInt64>(spec, options);
64 break;
66 addConfigSpecOption<VariantType::Float>(spec, options);
67 break;
69 addConfigSpecOption<VariantType::Double>(spec, options);
70 break;
72 addConfigSpecOption<VariantType::String>(spec, options);
73 break;
75 addConfigSpecOption<VariantType::Bool>(spec, options);
76 break;
78 addConfigSpecOption<VariantType::ArrayInt>(spec, options);
79 break;
81 addConfigSpecOption<VariantType::ArrayFloat>(spec, options);
82 break;
84 addConfigSpecOption<VariantType::ArrayDouble>(spec, options);
85 break;
87 addConfigSpecOption<VariantType::ArrayBool>(spec, options);
88 break;
90 addConfigSpecOption<VariantType::ArrayString>(spec, options);
91 break;
93 addConfigSpecOption<VariantType::Array2DInt>(spec, options);
94 break;
96 addConfigSpecOption<VariantType::Array2DFloat>(spec, options);
97 break;
99 addConfigSpecOption<VariantType::Array2DDouble>(spec, options);
100 break;
105 // FIXME: for Dict we should probably allow parsing stuff
106 // provided on the command line
110 break;
111 };
112 }
113}
114
116bool ConfigParamsHelper::hasOption(const std::vector<ConfigParamSpec>& specs, const std::string& optName)
117{
118 for (auto& old : specs) {
119 if (old.name == optName) {
120 return true;
121 }
122 }
123 return false;
124}
125
128void ConfigParamsHelper::addOptionIfMissing(std::vector<ConfigParamSpec>& specs, const ConfigParamSpec& spec)
129{
130 if (!hasOption(specs, spec.name)) {
131 specs.push_back(spec);
132 }
133}
134
137bool ConfigParamsHelper::dpl2BoostOptions(const std::vector<ConfigParamSpec>& spec,
138 boost::program_options::options_description& options,
139 boost::program_options::options_description vetos)
140{
141 bool haveOption = false;
142 for (const auto& configSpec : spec) {
143 // skip everything found in the veto definition
144 try {
145 if (vetos.find_nothrow(configSpec.name, false) != nullptr) {
146 continue;
147 }
148 } catch (boost::program_options::ambiguous_option& e) {
149 for (auto const& alternative : e.alternatives()) {
150 std::cerr << alternative << std::endl;
151 }
152 throw;
153 }
154
155 haveOption = true;
156 std::stringstream defaultValue;
157 defaultValue << configSpec.defaultValue;
158 if (configSpec.type != VariantType::Bool) {
159 if (configSpec.defaultValue.type() != VariantType::Empty) {
160 options.add_options()(configSpec.name.c_str(),
161 bpo::value<std::string>()->default_value(defaultValue.str()),
162 configSpec.help.c_str());
163 } else {
164 options.add_options()(configSpec.name.c_str(),
165 bpo::value<std::string>(),
166 configSpec.help.c_str());
167 }
168 } else {
169 if (configSpec.defaultValue.type() != VariantType::Empty) {
170 options.add_options()(configSpec.name.c_str(),
171 bpo::value<bool>()->zero_tokens()->default_value(configSpec.defaultValue.get<bool>()),
172 configSpec.help.c_str());
173 } else {
174 options.add_options()(configSpec.name.c_str(),
175 bpo::value<bool>()->zero_tokens(),
176 configSpec.help.c_str());
177 }
178 }
179 }
180
181 return haveOption;
182}
183
184} // namespace o2::framework
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
static bool hasOption(const std::vector< ConfigParamSpec > &specs, const std::string &optName)
Check if option is defined.
static void addOptionIfMissing(std::vector< ConfigParamSpec > &specs, const ConfigParamSpec &spec)
static bool dpl2BoostOptions(const std::vector< ConfigParamSpec > &spec, options_description &options, boost::program_options::options_description vetos=options_description())
static void populateBoostProgramOptions(options_description &options, const std::vector< ConfigParamSpec > &specs, options_description vetos=options_description())