Skip to content

Commit

Permalink
Detect incompatible options
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Feb 14, 2024
1 parent eb47cbe commit 9549d6c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/app/CmdOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ void optionDistance(Option& opt,
void optionStressTest(Option& opt,
CmdOptions& opts)
{
opts.option = OPTION_STRESS_TEST;
std::transform(opt.val.begin(), opt.val.end(), opt.val.begin(),
[](unsigned char c){ return std::toupper(c); });

Expand All @@ -285,6 +284,8 @@ void optionStressTest(Option& opt,
opts.stressTestMode = "RAM";
else
throw primesieve_error("invalid option '" + opt.str + "=" + opt.val + "'");

opts.setMainOption(OPTION_STRESS_TEST, opt.str);
}

/// Stress test timeout
Expand Down Expand Up @@ -372,7 +373,7 @@ CmdOptions parseOptions(int argc, char* argv[])
case OPTION_NO_STATUS: opts.status = false; break;
case OPTION_TIME: opts.time = true; break;
case OPTION_NUMBER: opts.numbers.push_back(opt.getValue<uint64_t>()); break;
default: opts.option = optionID;
default: opts.setMainOption(optionID, opt.str);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/app/CmdOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef CMDOPTIONS_HPP
#define CMDOPTIONS_HPP

#include <primesieve/primesieve_error.hpp>
#include <primesieve/Vector.hpp>
#include <stdint.h>
#include <string>
Expand Down Expand Up @@ -40,6 +41,7 @@ struct CmdOptions
{
primesieve::Vector<uint64_t> numbers;
std::string stressTestMode;
std::string optionStr;
int option = -1;
int flags = 0;
int sieveSize = 0;
Expand All @@ -50,6 +52,19 @@ struct CmdOptions
bool quiet = false;
bool status = true;
bool time = false;

void setMainOption(OptionID optionID,
const std::string& optStr)
{
// Multiple main options are not allowed
if (!optionStr.empty())
throw primesieve::primesieve_error("incompatible options: " + optionStr + " " + optStr);
else
{
optionStr = optStr;
option = optionID;
}
}
};

CmdOptions parseOptions(int, char**);
Expand Down

0 comments on commit 9549d6c

Please sign in to comment.