OpenLB 1.7
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 std::from_chars(str.data(), str.data() + str.size(), value);
67 return value;
68 } else {
69 return fallback;
70 }
71 }
72
73
74};
75
76template <>
77std::string CLIreader::getValueOrFallback<std::string>(
78 const std::string& name, std::string fallback) const {
79 if (contains(name)) {
80 return operator[](name);
81 } else {
82 return fallback;
83 }
84}
85
86}
87
88#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.