cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
parse.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_PARAMETER_PARSE_H
4#define pRC_CORE_PARAMETER_PARSE_H
5
6#include <cstring>
7
12
13namespace pRC
14{
15 template<class N, class D, class... Ps>
16 requires requires {
17 Logging::log(declval<N>());
18 Logging::log(declval<D>());
19 }
20 static inline constexpr auto parse(int const argc,
21 char const *const *const argv, N &&appName, D &&appDescription,
22 Ps &...parameters)
23 {
24 Logging::log(appName);
25 Logging::log(" ", appDescription);
26 Logging::log("");
27
28 for(int i = 1; i < argc; ++i)
29 {
30 if(std::strcmp(argv[i], "--help") == 0)
31 {
32 help(parameters...);
33 std::exit(0);
34 }
35
36 auto const parse = [&argc, &argv, &i](auto &parameter)
37 {
38 if constexpr(typename RemoveReference<
39 decltype(parameter)>::Context() ==
41 {
42 if(std::strcmp(argv[i], parameter.argument().cString()) ==
43 0)
44 {
45 ++i;
46 if(i >= argc)
47 {
48 auto const msg = "Missing value for parameter " +
49 parameter.name() + ": " + parameter.argument() +
50 " <" +
51 name<typename RemoveReference<
52 decltype(parameter)>::Type>() +
53 ">";
54 Logging::error(msg);
55 }
56 parameter.parseValue(argv[i]);
57 }
58 }
59 };
60
61 (parse(parameters), ...);
62 }
63
64 auto allSet = true;
65 auto const check = [&allSet](auto const &parameter)
66 {
67 if constexpr(typename RemoveReference<
68 decltype(parameter)>::Context() ==
70 {
71 if(!parameter.isSet())
72 {
73 Logging::log("Missing parameter:", parameter.name() + ":",
74 parameter.argument(),
75 "<" +
76 name<typename RemoveReference<
77 decltype(parameter)>::Type>() +
78 ">");
79
80 allSet = false;
81 }
82 }
83 };
84
85 (check(parameters), ...);
86
87 if(!allSet)
88 {
89 Logging::error("Some parameters have not been set!");
90 }
91
92 return report(parameters...);
93 }
94}
95#endif // pRC_CORE_PARAMETER_PARSE_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
int i
Definition gmock-matchers-comparisons_test.cc:603
static void log(X &&arg, Xs &&...args)
Definition io.hpp:17
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:10
std::remove_reference_t< T > RemoveReference
Definition basics.hpp:41
static constexpr auto report(Ps &&...parameters)
Definition report.hpp:12
static constexpr auto help(Ps &&...parameters)
Definition help.hpp:13
static constexpr auto parse(int const argc, char const *const *const argv, N &&appName, D &&appDescription, Ps &...parameters)
Definition parse.hpp:20
static constexpr auto parameter(A &&argument, N &&name, D &&description, T const &defaultValue)
Definition parameter.hpp:216
static constexpr auto name()
Definition type_name.hpp:11
Context
Definition context.hpp:9