matf-rg-engine 1.0.0
Base for project for the Computer Graphics course at Faculty of Mathematics, University of Belgrade
Loading...
Searching...
No Matches
ArgParser.hpp
Go to the documentation of this file.
1
6#ifndef ARGPARSER_HPP
7#define ARGPARSER_HPP
8#include <string_view>
9#include <optional>
10#include <string>
11
12namespace engine::util {
17 class ArgParser {
18 friend class App;
19
20 public:
21 static ArgParser *instance();
22
31 template<typename T>
32 std::optional<T> arg(std::string_view name, std::optional<T> default_value = {}) {
33 std::string arg_value = get_arg_value(name);
34 if (arg_value.empty()) {
35 return default_value.has_value() ? default_value.value() : T{};
36 }
37 std::size_t parsed = 0;
38 if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, int> || std::is_same_v<T, int32_t>) {
39 return std::stoi(arg_value, &parsed);
40 } else if constexpr (std::is_same_v<T, long long> || std::is_same_v<T, int64_t>) {
41 return std::stoll(arg_value, &parsed);
42 } else if constexpr (std::is_same_v<T, float>) {
43 return std::stof(arg_value, &parsed);
44 } else if constexpr (std::is_same_v<T, double>) {
45 return std::stod(arg_value, &parsed);
46 } else if constexpr (std::is_same_v<T, std::string>) {
47 return arg_value;
48 } else {
49 static_assert(false, "This type is not supported!");
50 }
51 }
52
58 void initialize(int argc, char **argv);
59
60 private:
66 std::string get_arg_value(std::string_view arg_name);
67
68 ArgParser() = default;
69
70 int m_argc = 0;
71 char **m_argv = nullptr;
72 };
73}
74#endif //ARGPARSER_HPP
Parses command line arguments.
Definition ArgParser.hpp:17
static ArgParser * instance()
Definition Utils.cpp:78
friend class App
Definition ArgParser.hpp:18
void initialize(int argc, char **argv)
Initialize the ArgParser with the command line arguments.
Definition Utils.cpp:83
int m_argc
Definition ArgParser.hpp:70
std::optional< T > arg(std::string_view name, std::optional< T > default_value={})
Get the value of an argument. Supported types: bool, int, long long, float, double,...
Definition ArgParser.hpp:32
char ** m_argv
Definition ArgParser.hpp:71
std::string get_arg_value(std::string_view arg_name)
Get the string value of an argument.
Definition Utils.cpp:89
Definition App.hpp:9