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 <string_view>
17#include <vector>
18#include <iostream>
19
20namespace bpo = boost::program_options;
21
22namespace o2::framework
23{
24
28 bpo::options_description& options,
29 const std::vector<ConfigParamSpec>& specs,
30 bpo::options_description vetos)
31{
32 auto proxy = options.add_options();
33 for (auto const& spec : specs) {
34 // skip everything found in the veto definition
35 if (vetos.find_nothrow(spec.name, false) != nullptr) {
36 continue;
37 }
38
39 switch (spec.type) {
40 // FIXME: Should we handle int and size_t diffently?
41 // FIXME: We should probably raise an error if the type is unknown
43 addConfigSpecOption<VariantType::Int>(spec, options);
44 break;
46 addConfigSpecOption<VariantType::Int8>(spec, options);
47 break;
49 addConfigSpecOption<VariantType::Int16>(spec, options);
50 break;
52 addConfigSpecOption<VariantType::Int64>(spec, options);
53 break;
55 addConfigSpecOption<VariantType::UInt8>(spec, options);
56 break;
58 addConfigSpecOption<VariantType::UInt16>(spec, options);
59 break;
61 addConfigSpecOption<VariantType::UInt32>(spec, options);
62 break;
64 addConfigSpecOption<VariantType::UInt64>(spec, options);
65 break;
67 addConfigSpecOption<VariantType::Float>(spec, options);
68 break;
70 addConfigSpecOption<VariantType::Double>(spec, options);
71 break;
73 addConfigSpecOption<VariantType::String>(spec, options);
74 break;
76 addConfigSpecOption<VariantType::Bool>(spec, options);
77 break;
79 addConfigSpecOption<VariantType::ArrayInt>(spec, options);
80 break;
82 addConfigSpecOption<VariantType::ArrayFloat>(spec, options);
83 break;
85 addConfigSpecOption<VariantType::ArrayDouble>(spec, options);
86 break;
88 addConfigSpecOption<VariantType::ArrayBool>(spec, options);
89 break;
91 addConfigSpecOption<VariantType::ArrayString>(spec, options);
92 break;
94 addConfigSpecOption<VariantType::Array2DInt>(spec, options);
95 break;
97 addConfigSpecOption<VariantType::Array2DFloat>(spec, options);
98 break;
100 addConfigSpecOption<VariantType::Array2DDouble>(spec, options);
101 break;
103 addConfigSpecOption<VariantType::LabeledArrayInt>(spec, options);
104 break;
106 addConfigSpecOption<VariantType::LabeledArrayFloat>(spec, options);
107 break;
109 addConfigSpecOption<VariantType::LabeledArrayDouble>(spec, options);
110 break;
112 addConfigSpecOption<VariantType::LabeledArrayString>(spec, options);
113 break;
114 // FIXME: for Dict we should probably allow parsing stuff
115 // provided on the command line
119 break;
120 };
121 }
122}
123
124std::string ConfigParamsHelper::defaultValueForHelp(std::string const& value)
125{
126 constexpr std::string_view ellipsis = "...";
127 if (value.size() <= MaxHelpDefaultLength) {
128 return value;
129 }
130 // Keep more of the beginning, which identifies the value, and the end, which closes it
131 constexpr size_t kept = MaxHelpDefaultLength - ellipsis.size();
132 constexpr size_t tail = kept / 3;
133 constexpr size_t head = kept - tail;
134 return value.substr(0, head) + std::string{ellipsis} + value.substr(value.size() - tail);
135}
136
138bool ConfigParamsHelper::hasOption(const std::vector<ConfigParamSpec>& specs, const std::string& optName)
139{
140 for (auto& old : specs) {
141 if (old.name == optName) {
142 return true;
143 }
144 }
145 return false;
146}
147
150void ConfigParamsHelper::addOptionIfMissing(std::vector<ConfigParamSpec>& specs, const ConfigParamSpec& spec)
151{
152 if (!hasOption(specs, spec.name)) {
153 specs.push_back(spec);
154 }
155}
156
159bool ConfigParamsHelper::dpl2BoostOptions(const std::vector<ConfigParamSpec>& spec,
160 boost::program_options::options_description& options,
161 boost::program_options::options_description const& vetos)
162{
163 bool haveOption = false;
164 for (const auto& configSpec : spec) {
165 // skip everything found in the veto definition
166 try {
167 if (vetos.find_nothrow(configSpec.name, false) != nullptr) {
168 continue;
169 }
170 } catch (boost::program_options::ambiguous_option& e) {
171 for (auto const& alternative : e.alternatives()) {
172 std::cerr << alternative << std::endl;
173 }
174 throw;
175 }
176
177 haveOption = true;
178 std::stringstream defaultValue;
179 defaultValue << configSpec.defaultValue;
180 if (configSpec.type != VariantType::Bool) {
181 if (configSpec.defaultValue.type() != VariantType::Empty) {
182 options.add_options()(configSpec.name.c_str(),
183 bpo::value<std::string>()->default_value(defaultValue.str(), defaultValueForHelp(defaultValue.str())),
184 configSpec.help.c_str());
185 } else {
186 options.add_options()(configSpec.name.c_str(),
187 bpo::value<std::string>(),
188 configSpec.help.c_str());
189 }
190 } else {
191 if (configSpec.defaultValue.type() != VariantType::Empty) {
192 options.add_options()(configSpec.name.c_str(),
193 bpo::value<bool>()->zero_tokens()->default_value(configSpec.defaultValue.get<bool>()),
194 configSpec.help.c_str());
195 } else {
196 options.add_options()(configSpec.name.c_str(),
197 bpo::value<bool>()->zero_tokens(),
198 configSpec.help.c_str());
199 }
200 }
201 }
202
203 return haveOption;
204}
205
206} // namespace o2::framework
GLsizei const GLfloat * value
Definition glcorearb.h:819
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
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 constexpr size_t MaxHelpDefaultLength
static bool dpl2BoostOptions(const std::vector< ConfigParamSpec > &spec, options_description &options, boost::program_options::options_description const &vetos=options_description())
static void populateBoostProgramOptions(options_description &options, const std::vector< ConfigParamSpec > &specs, options_description vetos=options_description())
static std::string defaultValueForHelp(std::string const &value)