Project
Loading...
Searching...
No Matches
ConfigParamsHelper.h
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#ifndef FRAMEWORK_CONFIGPARAMSHELPER_H
12#define FRAMEWORK_CONFIGPARAMSHELPER_H
13
15#include <boost/program_options.hpp>
16
17#include <string>
18#include <vector>
19#include <string>
20#include <type_traits>
21
22namespace o2::framework
23{
24
25using options_description = boost::program_options::options_description;
26
29 options_description& options,
30 const std::vector<ConfigParamSpec>& specs,
32
36 static bool dpl2BoostOptions(const std::vector<ConfigParamSpec>& spec,
37 options_description& options,
38 boost::program_options::options_description vetos = options_description());
39
41 static bool hasOption(const std::vector<ConfigParamSpec>& specs, const std::string& optName);
42
45 static void addOptionIfMissing(std::vector<ConfigParamSpec>& specs, const ConfigParamSpec& spec);
46
48 template <typename ContainerType>
49 static boost::program_options::options_description
50 prepareOptionDescriptions(ContainerType const& workflow,
51 std::vector<ConfigParamSpec> const& currentWorkflowOptions,
53 std::string mode = "full")
54 {
55 boost::program_options::options_description toplevel;
56 boost::program_options::options_description wo("Global workflow options");
57 if (dpl2BoostOptions(currentWorkflowOptions, wo, vetos)) {
58 toplevel.add(wo);
59 }
60 std::string specOptionsDescription = "Available data processors";
61 if (mode == "short") {
62 specOptionsDescription += " (full info with '--help full')";
63 }
64 options_description specOptions(specOptionsDescription);
65 for (const auto& spec : workflow) {
66 std::string name = "Data processor options: " + spec.name;
67 boost::program_options::options_description processorOptions(name);
68 if (dpl2BoostOptions(spec.options, processorOptions, vetos)) {
69 // if vetos have been provided to the function we also need to make
70 // sure that there are no duplicate option definitions for the individual
71 // processor specs, so we add in order to be vetos for all subsequent specs.
72 // Note: this only concerns the main parser, all individual options are
73 // handled when starting individual processors.
74 if (vetos.options().size() > 0) {
75 vetos.add(processorOptions);
76 }
77 if (mode == "full") {
78 specOptions.add(processorOptions);
79 } else if (mode == spec.name) {
80 toplevel.add(processorOptions);
81 break;
82 }
83 }
84 if (mode == "full" || mode == "short") {
85 std::string help;
86 if (mode == "full") {
87 help = "Option groups by process name: --" + spec.name + R"( "<processor options>")";
88 } else if (mode == "short" && processorOptions.options().size() > 0) {
89 help = "Use '--help " + spec.name + "' to display processor options";
90 } else if (mode == "short" && processorOptions.options().size() == 0) {
91 help = "No processor options";
92 }
93 specOptions.add_options()(spec.name.c_str(),
94 boost::program_options::value<std::string>(),
95 help.c_str());
96 }
97 }
98 if (workflow.size() > 0 && (mode == "full" || mode == "short")) {
99 toplevel.add(specOptions);
100 }
101 return toplevel;
102 }
103
104 template <VariantType V>
105 static void addConfigSpecOption(const ConfigParamSpec& spec,
106 boost::program_options::options_description& options)
107 {
108 const char* name = spec.name.c_str();
109 const char* help = spec.help.c_str();
110
111 if constexpr (isSimpleVariant<V>()) {
112 using Type = typename variant_type<V>::type;
113 using BoostType = typename std::conditional<V == VariantType::String, std::string, Type>::type;
114 auto value = boost::program_options::value<BoostType>();
115 value = value->default_value(spec.defaultValue.get<BoostType>());
116 if constexpr (V == VariantType::Bool) {
117 // for bool values we also support the zero_token option to make
118 // the option usable as a single switch
119 value = value->zero_tokens();
120 }
121 options.add_options()(name, value, help);
122 } else if constexpr (isArray<V>() || isArray2D<V>()) {
123 auto value = boost::program_options::value<std::string>();
124 value = value->default_value(spec.defaultValue.asString());
125 if constexpr (V != VariantType::String) {
126 value = value->multitoken();
127 }
128 options.add_options()(name, value, help);
129 } else {
130 using Type = typename variant_type<V>::type;
131 using BoostType = typename std::conditional<V == VariantType::String, std::string, Type>::type;
132 auto value = boost::program_options::value<BoostType>();
133 options.add_options()(name, value, help);
134 }
135 }
136};
137
138} // namespace o2::framework
139#endif // FRAMEWORK_CONFIGPARAMSHELPER_H
std::string asString() const
Definition Variant.cxx:80
GLenum mode
Definition glcorearb.h:266
GLuint const GLchar * name
Definition glcorearb.h:781
GLsizei const GLfloat * value
Definition glcorearb.h:819
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
boost::program_options::options_description options_description
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 void addConfigSpecOption(const ConfigParamSpec &spec, boost::program_options::options_description &options)
static boost::program_options::options_description prepareOptionDescriptions(ContainerType const &workflow, std::vector< ConfigParamSpec > const &currentWorkflowOptions, options_description vetos=options_description(), std::string mode="full")
populate boost program options for a complete workflow
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())