cMHN 1.1
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
10#include <prc/core/log/log.hpp>
13
14namespace pRC
15{
16 template<class N, class D, class... Ps,
19 static inline constexpr auto parse(int const argc,
20 char const *const *const argv, N &&appName, D &&appDescription,
21 Ps &...parameters)
22 {
25 Logging::log("");
26
27 for(int i = 1; i < argc; ++i)
28 {
29 if(std::strcmp(argv[i], "--help") == 0)
30 {
31 help(parameters...);
32 std::exit(0);
33 }
34
35 auto const parse = [&argc, &argv, &i](auto &parameter)
36 {
37 if constexpr(typename RemoveReference<
38 decltype(parameter)>::Context() ==
40 {
41 if(std::strcmp(argv[i], parameter.argument().cString()) ==
42 0)
43 {
44 ++i;
45 if(i >= argc)
46 {
47 auto const msg = "Missing value for parameter " +
48 parameter.name() + ": " + parameter.argument() +
49 " <" +
50 name<typename RemoveReference<
51 decltype(parameter)>::Type>() +
52 ">";
54 }
55 parameter.parseValue(argv[i]);
56 }
57 }
58 };
59
60 (parse(parameters), ...);
61 }
62
63 auto allSet = true;
64 auto const check = [&allSet](auto const &parameter)
65 {
66 if constexpr(typename RemoveReference<
67 decltype(parameter)>::Context() ==
69 {
70 if(!parameter.isSet())
71 {
72 Logging::log("Missing parameter:", parameter.name() + ":",
73 parameter.argument(),
74 "<" +
75 name<typename RemoveReference<
76 decltype(parameter)>::Type>() +
77 ">");
78
79 allSet = false;
80 }
81 }
82 };
83
84 (check(parameters), ...);
85
86 if(!allSet)
87 {
88 Logging::error("Some parameters have not been set!");
89 }
90
91 return report(parameters...);
92 }
93}
94#endif // pRC_CORE_PARAMETER_PARSE_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
static void log(X &&arg, Xs &&...args)
Definition io.hpp:17
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
std::remove_reference_t< T > RemoveReference
Definition type_traits.hpp:56
static constexpr auto parse(int const argc, char const *const *const argv, N &&appName, D &&appDescription, Ps &...parameters)
Definition parse.hpp:19
static constexpr auto report(Ps &&...parameters)
Definition report.hpp:13
static constexpr auto help(Ps &&...parameters)
Definition help.hpp:14
static constexpr auto name()
Definition type_name.hpp:11
Context
Definition context.hpp:9
static constexpr auto parameter(A &&argument, N &&name, D &&description, T const &defaultValue)
Definition parameter.hpp:215