Skip to content

Commit

Permalink
cli tools: add more helpful error messages when parsing cli arguments
Browse files Browse the repository at this point in the history
closes #35
  • Loading branch information
foolnotion committed Mar 19, 2024
1 parent 8a12b9e commit 59ea4c1
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions cli/source/operator_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ namespace Operon { struct MutatorBase; }
namespace Operon { struct Variable; }

namespace Operon {

namespace detail {
auto GetErrorString(std::string const& name, std::string const& arg) {
return fmt::format("unable to parse {} argument '{}'", name, arg);
}
} // namespace detail

auto ParseReinserter(std::string const& str, ComparisonCallback&& comp) -> std::unique_ptr<ReinserterBase>
{
std::unique_ptr<ReinserterBase> reinserter;
if (str == "keep-best") {
reinserter = std::make_unique<KeepBestReinserter>(std::move(comp));
} else if (str == "replace-worst") {
reinserter = std::make_unique<ReplaceWorstReinserter>(std::move(comp));
} else {
throw std::invalid_argument(detail::GetErrorString("reinserter", str));
}
return reinserter;
}
Expand All @@ -56,6 +65,8 @@ auto ParseSelector(std::string const& str, ComparisonCallback&& comp) -> std::un
dynamic_cast<Operon::RankTournamentSelector*>(selector.get())->SetTournamentSize(tournamentSize);
} else if (name == "random") {
selector = std::make_unique<Operon::RandomSelector>();
} else {
throw std::invalid_argument(detail::GetErrorString("selector", str));
}

return selector;
Expand All @@ -77,6 +88,8 @@ auto ParseCreator(std::string const& str, PrimitiveSet const& pset, std::vector<
creator = std::make_unique<ProbabilisticTreeCreator>(pset, inputs, bias);
} else if (str == "grow") {
creator = std::make_unique<GrowTreeCreator>(pset, inputs);
} else {
throw std::invalid_argument(detail::GetErrorString("creator", str));
}
return creator;
}
Expand All @@ -103,34 +116,11 @@ auto ParseEvaluator(std::string const& str, Problem& problem, DefaultDispatch& d
} else if (str == "gauss") {
evaluator = std::make_unique<Operon::GaussianLikelihoodEvaluator<T>>(problem, dtable);
} else {
throw std::runtime_error(fmt::format("Unknown metric {}\n", str));
throw std::runtime_error(fmt::format("unable to parse evaluator metric '{}'\n", str));
}
return evaluator;
}

// auto ParseErrorMetric(std::string const& str) -> std::tuple<std::unique_ptr<ErrorMetric>, bool>
// {
// std::unique_ptr<ErrorMetric> error;
// bool scale{true};
// if (str == "r2") {
// error = std::make_unique<Operon::R2>();
// } else if (str == "c2") {
// scale = false;
// error = std::make_unique<Operon::C2>();
// } else if (str == "nmse") {
// error = std::make_unique<Operon::NMSE>();
// } else if (str == "mse") {
// error = std::make_unique<Operon::MSE>();
// } else if (str == "rmse") {
// error = std::make_unique<Operon::RMSE>();
// } else if (str == "mae") {
// error = std::make_unique<Operon::MAE>();
// } else {
// throw std::runtime_error(fmt::format("Unknown metric {}\n", str));
// }
// return std::make_tuple(std::move(error), scale);
// }

auto ParseGenerator(std::string const& str, EvaluatorBase& eval, CrossoverBase& cx, MutatorBase& mut, SelectorBase& femSel, SelectorBase& maleSel) -> std::unique_ptr<OffspringGeneratorBase>
{
std::unique_ptr<OffspringGeneratorBase> generator;
Expand All @@ -156,6 +146,8 @@ auto ParseGenerator(std::string const& str, EvaluatorBase& eval, CrossoverBase&
size_t polygenicSize{PolygenicOffspringGenerator::DefaultBroodSize};
if (tok.size() > 1) { (void) scn::scan(tok[1], "{}", polygenicSize); }
dynamic_cast<PolygenicOffspringGenerator*>(generator.get())->PolygenicSize(polygenicSize);
} else {
throw std::invalid_argument(detail::GetErrorString("generator", str));
}
return generator;
}
Expand Down

0 comments on commit 59ea4c1

Please sign in to comment.