OpenLB 1.8.1
Loading...
Searching...
No Matches
cliReader.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2023 Adrian Kummerlaender
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef IO_CLI_READER_H_
25#define IO_CLI_READER_H_
26
27#include <string>
28#include <vector>
29#include <charconv>
30
31namespace olb {
32
34class CLIreader {
35private:
36 std::vector<std::string> _tokens;
37
38public:
39 CLIreader(int& argc, char** argv) {
40 for (int i=1; i < argc; ++i) {
41 _tokens.emplace_back(argv[i]);
42 }
43 }
44
46 bool contains(const std::string& name) const {
47 return std::find(_tokens.begin(), _tokens.end(), name) != _tokens.end();
48 }
49
51 std::string operator[](const std::string& name) const {
52 auto iter = std::find(_tokens.begin(), _tokens.end(), name);
53 if (iter != _tokens.end() && ++iter != _tokens.end()){
54 return *iter;
55 } else {
56 return std::string{};
57 }
58 }
59
61 template <typename TYPE>
62 TYPE getValueOrFallback(const std::string& name, TYPE fallback) const {
63 if (contains(name)) {
64 const std::string str = operator[](name);
65 TYPE value{};
66 // Handling required due to inconsistent support for std::from_chars
67 if constexpr (std::is_integral_v<TYPE>) {
68 auto result = std::from_chars(str.data(),
69 str.data() + str.size(),
70 value);
71 if (result.ec != std::errc()) {
72 return fallback;
73 }
74 return value;
75 } else if constexpr (std::is_floating_point_v<TYPE>) {
76 try {
77 std::size_t nProcessed{};
78 value = static_cast<TYPE>(std::stod(str, &nProcessed));
79 if (nProcessed != str.size()) {
80 return fallback;
81 }
82 return value;
83 } catch (...) {
84 return fallback;
85 }
86 } else {
87 static_assert([](){ return false; }, "Unsupported type");
88 }
89 } else {
90 return fallback;
91 }
92 }
93
94};
95
96template <>
97std::string CLIreader::getValueOrFallback<std::string>(
98 const std::string& name, std::string fallback) const {
99 if (contains(name)) {
100 return operator[](name);
101 } else {
102 return fallback;
103 }
104}
105
106}
107
108#endif
Very simple CLI argument parser.
Definition cliReader.h:34
CLIreader(int &argc, char **argv)
Definition cliReader.h:39
std::string operator[](const std::string &name) const
Returns value of token after name (i.e. by convention the value assigned to name)
Definition cliReader.h:51
TYPE getValueOrFallback(const std::string &name, TYPE fallback) const
Return value of name as TYPE or fallback if not provided.
Definition cliReader.h:62
bool contains(const std::string &name) const
Returns true iff name is specified.
Definition cliReader.h:46
Top level namespace for all of OpenLB.