Skip to content

Commit adf91d9

Browse files
committed
DPL: fix handling of boolean options
1 parent d813052 commit adf91d9

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,5 @@ O2_GENERATE_TESTS(
263263
COMMAND_LINE_ARGS
264264
--global-config require-me
265265
# Note: the group switch makes process consumer parse only the group arguments
266-
--consumer "--global-config consumer-config --local-option hello-aliceo2"
266+
--consumer "--global-config consumer-config --local-option hello-aliceo2 --a-boolean3 --an-int2 20 --a-double2 22."
267267
)

Framework/Core/src/BoostOptionsRetriever.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ BoostOptionsRetriever::BoostOptionsRetriever(std::vector<ConfigParamSpec> const&
5151
options = options(name, bpo::value<std::string>()->default_value(spec.defaultValue.get<const char *>()), help);
5252
break;
5353
case VariantType::Bool:
54-
options = options(name, bpo::value<bool>()->default_value(spec.defaultValue.get<bool>()), help);
54+
options = options(name, bpo::value<bool>()->zero_tokens()->default_value(spec.defaultValue.get<bool>()), help);
5555
break;
5656
case VariantType::Unknown:
5757
case VariantType::Empty:

Framework/Core/src/ConfigParamsHelper.cxx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,27 @@ void ConfigParamsHelper::populateBoostProgramOptions(
6363

6464
/// populate boost program options making all options of type string
6565
/// this is used for filtering the command line argument
66-
bool ConfigParamsHelper::prepareOptionsDescription(const std::vector<ConfigParamSpec> &spec,
66+
bool ConfigParamsHelper::prepareOptionsDescription(const std::vector<ConfigParamSpec>& spec,
6767
boost::program_options::options_description& options,
68-
boost::program_options::options_description vetos
69-
)
68+
boost::program_options::options_description vetos)
7069
{
7170
bool haveOption = false;
72-
for (const auto & configSpec : spec) {
71+
for (const auto& configSpec : spec) {
7372
// skip everything found in the veto definition
74-
if (vetos.find_nothrow(configSpec.name, false)) continue;
73+
if (vetos.find_nothrow(configSpec.name, false))
74+
continue;
7575
haveOption = true;
7676
std::stringstream defaultValue;
7777
defaultValue << configSpec.defaultValue;
78-
options.add_options()
79-
(configSpec.name.c_str(),
80-
bpo::value<std::string>()->default_value(defaultValue.str().c_str()),
81-
configSpec.help.c_str());
78+
if (configSpec.type != VariantType::Bool) {
79+
options.add_options()(configSpec.name.c_str(),
80+
bpo::value<std::string>()->default_value(defaultValue.str().c_str()),
81+
configSpec.help.c_str());
82+
} else {
83+
options.add_options()(configSpec.name.c_str(),
84+
bpo::value<bool>()->zero_tokens()->default_value(configSpec.defaultValue.get<bool>()),
85+
configSpec.help.c_str());
86+
}
8287
}
8388

8489
return haveOption;

Framework/Core/src/DeviceSpecHelpers.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,6 @@ void DeviceSpecHelpers::prepareArguments(int argc, char** argv, bool defaultQuie
628628
// and is not defaulted
629629
const auto* description = odesc.find_nothrow(varit.first, false);
630630
if (description && varmap.count(varit.first)) {
631-
tmpArgs.emplace_back("--");
632-
tmpArgs.back() += varit.first;
633631
// check the semantics of the value
634632
auto semantic = description->semantic();
635633
const char* optarg = "";
@@ -638,11 +636,16 @@ void DeviceSpecHelpers::prepareArguments(int argc, char** argv, bool defaultQuie
638636
// multitoken, zero_token and composing
639637
// currently only the simple case is supported
640638
assert(semantic->min_tokens() <= 1);
641-
assert(semantic->max_tokens() && semantic->min_tokens());
639+
//assert(semantic->max_tokens() && semantic->min_tokens());
642640
if (semantic->min_tokens() > 0) {
641+
tmpArgs.emplace_back("--");
642+
tmpArgs.back() += varit.first;
643643
// add the token
644644
tmpArgs.emplace_back(varit.second.as<std::string>());
645645
optarg = tmpArgs.back().c_str();
646+
} else if (semantic->min_tokens() == 0 && varit.second.as<bool>()) {
647+
tmpArgs.emplace_back("--");
648+
tmpArgs.back() += varit.first;
646649
}
647650
}
648651
control.options.insert(std::make_pair(varit.first, optarg));

Framework/Core/test/test_ProcessorOptions.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ WorkflowSpec defineDataProcessing(ConfigContext const&)
6262
// read back the option from the command line, see CMakeLists.txt
6363
auto configstring = ic.options().get<std::string>("global-config");
6464
auto anotheroption = ic.options().get<std::string>("local-option");
65+
auto aBoolean = ic.options().get<bool>("a-boolean");
66+
auto aBoolean2 = ic.options().get<bool>("a-boolean2");
67+
auto aBoolean3 = ic.options().get<bool>("a-boolean3");
68+
auto anInt = ic.options().get<int>("an-int");
69+
auto anInt2 = ic.options().get<int>("an-int2");
70+
auto aDouble = ic.options().get<double>("a-double");
71+
auto aDouble2 = ic.options().get<double>("a-double2");
72+
73+
ASSERT_ERROR(aBoolean == true);
74+
ASSERT_ERROR(aBoolean2 == false);
75+
ASSERT_ERROR(aBoolean3 == true);
76+
ASSERT_ERROR(anInt == 10);
77+
ASSERT_ERROR(anInt2 == 20);
78+
ASSERT_ERROR(aDouble == 11.);
79+
ASSERT_ERROR(aDouble2 == 22.);
6580
ASSERT_ERROR(configstring == "consumer-config");
6681
ASSERT_ERROR(anotheroption == "hello-aliceo2");
6782

@@ -74,6 +89,13 @@ WorkflowSpec defineDataProcessing(ConfigContext const&)
7489
{
7590
ConfigParamSpec{ "global-config", VariantType::String, { "A global config option for all processor specs" } },
7691
ConfigParamSpec{ "local-option", VariantType::String, { "Option only valid for this processor spec" } },
92+
ConfigParamSpec{ "a-boolean", VariantType::Bool, true, { "A boolean which we pick by default" } },
93+
ConfigParamSpec{ "a-boolean2", VariantType::Bool, false, { "Another boolean which we pick by default" } },
94+
ConfigParamSpec{ "a-boolean3", VariantType::Bool, false, { "Another boolean which we pick from the outside options" } },
95+
ConfigParamSpec{ "an-int", VariantType::Int, 10, { "An int for which we pick up the default" } },
96+
ConfigParamSpec{ "an-int2", VariantType::Int, 1, { "An int for which we pick up the override" } },
97+
ConfigParamSpec{ "a-double", VariantType::Double, 11., { "A double for which we pick up the override" } },
98+
ConfigParamSpec{ "a-double2", VariantType::Double, 12., { "A double for which we pick up the override" } },
7799
},
78100
}
79101
};

0 commit comments

Comments
 (0)