cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
argsHandler.cpp
Go to the documentation of this file.
1#include "CAE/ArgsHandler.hpp"
2#include "CAE/Common.hpp"
3
4#include "Utils/Env.hpp"
5
6#include <iostream>
7
8cae::ArgsConfig cae::ArgsHandler::ParseArgs(const int argc, const char *const *argv)
9{
10 ArgsConfig config;
11 config.run = true;
12
13 if (argc <= 1)
14 {
15 return config;
16 }
17
18 for (int i = 1; i < argc; ++i)
19 {
20 std::string arg = argv[i];
21
22 if (arg == "-h" || arg == "--help")
23 {
24 std::cout << MESSAGE::HELP_MSG;
25 config.run = false;
26 return config;
27 }
28 if (arg == "-v" || arg == "--version")
29 {
30 std::cout << MESSAGE::VERSION_MSG;
31 config.run = false;
32 return config;
33 }
34 if (arg == "-c" || arg == "--config")
35 {
36 if (i + 1 >= argc)
37 {
38 throw std::runtime_error("Missing value for argument " + arg);
39 }
40
41 config.config_path = argv[++i];
42 }
43 else
44 {
45 throw std::runtime_error("Unknown argument: " + arg + ". Use -h or --help to see available options.");
46 }
47 }
48
49 return config;
50}
51
53{
54 EnvConfig config;
55 const auto envMap = utl::getEnvMap(envp);
56
57 if (envMap.contains("USER"))
58 {
59 config.user_name = envMap.at("USER");
60 }
61 if (envMap.contains("PWD"))
62 {
63 config.pwd = envMap.at("PWD");
64 }
65
66 return config;
67}
This file contains the ArgsHandler class declaration.
This file contains env utility functions.
static ArgsConfig ParseArgs(int argc, const char *const *argv)
Parse command line arguments.
static EnvConfig ParseEnv(const char *const *envp)
Parse environment variables.
This file contains the common definitions.
static constexpr std::string_view VERSION_MSG
Definition Common.hpp:22
static constexpr std::string_view HELP_MSG
Definition Common.hpp:17
std::unordered_map< std::string, std::string > getEnvMap(const char *const *env)
Get environment variables as a map.
Definition env.cpp:9
Struct for command line arguments configuration.
std::string config_path
Struct for environment variables configuration.
std::string user_name
std::string pwd