CommandLineParser: Refactor checkMutuallyExclusive() to handle multiple options and use it more

This commit is contained in:
Kamil Śliwak 2021-06-13 14:28:03 +02:00
parent f97fe813ec
commit 3b104a3f38
2 changed files with 9 additions and 18 deletions

View File

@ -198,14 +198,14 @@ void CommandLineParser::printLicenseAndExit()
} }
bool CommandLineParser::checkMutuallyExclusive(boost::program_options::variables_map const& args, string const& _optionA, string const& _optionB) bool CommandLineParser::checkMutuallyExclusive(vector<string> const& _optionNames)
{ {
if (args.count(_optionA) && args.count(_optionB)) if (countEnabledOptions(_optionNames) > 1)
{ {
serr() << "Option " << _optionA << " and " << _optionB << " are mutually exclusive." << endl; serr() << "The following options are mutually exclusive: " << joinOptionNames(_optionNames) << ". ";
serr() << "Select at most one." << endl;
return false; return false;
} }
return true; return true;
} }
@ -735,7 +735,7 @@ General Information)").c_str(),
return false; return false;
} }
if (!checkMutuallyExclusive(m_args, g_strColor, g_strNoColor)) if (!checkMutuallyExclusive({g_strColor, g_strNoColor}))
return false; return false;
array<string, 8> const conflictingWithStopAfter{ array<string, 8> const conflictingWithStopAfter{
@ -750,7 +750,7 @@ General Information)").c_str(),
}; };
for (auto& option: conflictingWithStopAfter) for (auto& option: conflictingWithStopAfter)
if (!checkMutuallyExclusive(m_args, g_strStopAfter, option)) if (!checkMutuallyExclusive({g_strStopAfter, option}))
return false; return false;
if (m_args.count(g_strColor) > 0) if (m_args.count(g_strColor) > 0)
@ -861,20 +861,15 @@ General Information)").c_str(),
m_options.output.stopAfter = CompilerStack::State::Parsed; m_options.output.stopAfter = CompilerStack::State::Parsed;
} }
vector<string> const exclusiveModes = { if (!checkMutuallyExclusive({
g_strStandardJSON, g_strStandardJSON,
g_strLink, g_strLink,
g_strAssemble, g_strAssemble,
g_strStrictAssembly, g_strStrictAssembly,
g_strYul, g_strYul,
g_strImportAst, g_strImportAst,
}; }))
if (countEnabledOptions(exclusiveModes) > 1)
{
serr() << "The following options are mutually exclusive: " << joinOptionNames(exclusiveModes) << ". ";
serr() << "Select at most one." << endl;
return false; return false;
}
if (m_args.count(g_strStandardJSON)) if (m_args.count(g_strStandardJSON))
{ {

View File

@ -218,11 +218,7 @@ private:
/// @return false if there are any validation errors, true otherwise. /// @return false if there are any validation errors, true otherwise.
bool parseLibraryOption(std::string const& _input); bool parseLibraryOption(std::string const& _input);
bool checkMutuallyExclusive( bool checkMutuallyExclusive(std::vector<std::string> const& _optionNames);
boost::program_options::variables_map const& args,
std::string const& _optionA,
std::string const& _optionB
);
[[noreturn]] void printVersionAndExit(); [[noreturn]] void printVersionAndExit();
[[noreturn]] void printLicenseAndExit(); [[noreturn]] void printLicenseAndExit();
size_t countEnabledOptions(std::vector<std::string> const& _optionNames) const; size_t countEnabledOptions(std::vector<std::string> const& _optionNames) const;