Skip to content

Commit 3a6573a

Browse files
committed
Integrate AppMode
1 parent b4e1d6a commit 3a6573a

1 file changed

Lines changed: 82 additions & 17 deletions

File tree

‎src/main.cpp‎

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#include <exception>
12
#include <iostream>
23
#include <limits>
34
#include <stdexcept>
5+
#include <string>
46
#include <vector>
57

68
#include "ExampleRegistry.h"
@@ -34,8 +36,9 @@ void runMenu() {
3436
}
3537

3638
// Group
37-
int gIndex = 1;
39+
size_t g_index = 1;
3840
std::vector<std::string> groups;
41+
groups.reserve(data.size());
3942

4043
for (const auto& [group, _] : data) {
4144
groups.push_back(group);
@@ -44,50 +47,50 @@ void runMenu() {
4447
// sort
4548
std::sort(groups.begin(), groups.end());
4649
for (const auto& group : groups) {
47-
std::cout << gIndex++ << ". " << group << "\n";
50+
std::cout << g_index++ << ". " << group << "\n";
4851
}
49-
std::cout << gIndex << ". Exit\n";
52+
std::cout << g_index << ". Exit\n";
5053
std::cout << "----------------------------------------\n";
5154
std::cout << "Enter choice: ";
5255

53-
int gChoice = readChoice();
54-
if (gChoice == gIndex) {
56+
size_t g_choice = readChoice();
57+
if (g_choice == g_index) {
5558
std::cout << "\n--- Exit ---\n";
5659
break;
5760
}
5861

59-
if (gChoice < 1 || gChoice > groups.size()) {
62+
if (g_choice < 1 || g_choice > groups.size()) {
6063
std::cout << "Invalid group choice\n";
6164
continue;
6265
}
6366

6467
try {
6568
// Sub-group
66-
const auto& selectedGroup = groups[gChoice - 1];
67-
const auto& examples = data.at(selectedGroup);
69+
const auto& selected_group = groups[g_choice - 1];
70+
const auto& examples = data.at(selected_group);
6871

69-
int eIndex = 1;
72+
int e_index = 1;
7073
std::vector<std::string> names;
7174

7275
for (const auto& [name, _] : examples) {
73-
std::cout << eIndex++ << ". " << name << "\n";
76+
std::cout << e_index++ << ". " << name << "\n";
7477
names.push_back(name);
7578
}
76-
std::cout << eIndex << ". Back\n";
79+
std::cout << e_index << ". Back\n";
7780
std::cout << "----------------------------------------\n";
7881
std::cout << "Enter choice: ";
7982

80-
int eChoice = readChoice();
81-
if (eChoice == eIndex) {
83+
int e_choice = readChoice();
84+
if (e_choice == e_index) {
8285
continue;
8386
}
8487

85-
if (eChoice < 1 || eChoice > eIndex) {
88+
if (e_choice < 1 || e_choice > e_index) {
8689
std::cout << "Invalid example choice\n";
8790
continue;
8891
}
8992

90-
auto example = registry.create(selectedGroup, names[eChoice - 1]);
93+
auto example = registry.create(selected_group, names[e_choice - 1]);
9194
if (example != nullptr) {
9295
std::cout << "\n--- Running Example [" << example->name() << "] ["
9396
<< example->description() << "]---\n\n";
@@ -103,8 +106,7 @@ void runMenu() {
103106
}
104107
}
105108

106-
int main(int argc, char* argv[]) {
107-
LOG("Logger has been integrated");
109+
void printInfo() {
108110
std::cout << std::endl;
109111
if (__cplusplus == 202302L)
110112
std::cout << "C++23";
@@ -123,6 +125,69 @@ int main(int argc, char* argv[]) {
123125
std::cout << "\n";
124126
std::cout << APP_NAME << " v" << APP_VERSION << std::endl;
125127
std::cout << APP_DESCRIPTION << std::endl;
128+
}
129+
130+
enum class ApplicationMode {
131+
kDev,
132+
kUAT,
133+
kProd,
134+
};
135+
136+
static ApplicationMode mode = ApplicationMode::kDev;
137+
138+
std::string toString(ApplicationMode m) {
139+
switch (m) {
140+
case ApplicationMode::kDev:
141+
return "dev";
142+
case ApplicationMode::kUAT:
143+
return "uat";
144+
case ApplicationMode::kProd:
145+
return "prod";
146+
}
147+
return "unknown";
148+
}
149+
150+
ApplicationMode parseMode(const std::string& value) {
151+
if (value == "Dev")
152+
return ApplicationMode::kDev;
153+
if (value == "Uat")
154+
return ApplicationMode::kUAT;
155+
if (value == "Prod")
156+
return ApplicationMode::kProd;
157+
158+
throw std::invalid_argument("Invalid mode: " + value +
159+
". Expected: dev, uat, prod");
160+
}
161+
162+
void handleArg(int argc, const char* const argv[]) {
163+
// argv[0] = executable
164+
for (int i = 1; i < argc; ++i) {
165+
std::string arg = argv[i];
166+
167+
if (arg == "-mode") {
168+
if (i + 1 >= argc) {
169+
std::cerr << "Missing value for -mode\n";
170+
return;
171+
}
172+
173+
std::string value = argv[++i];
174+
try {
175+
mode = parseMode(value);
176+
} catch (const std::exception& e) {
177+
std::cerr << e.what() << '\n';
178+
}
179+
} else {
180+
std::cerr << "Invalid argv param: " << arg << "\n";
181+
}
182+
}
183+
}
184+
185+
int main(int argc, char* argv[]) {
186+
handleArg(argc, argv);
187+
LOG("Running in mode: " + toString(mode));
188+
LOG("Logger has been integrated");
189+
printInfo();
126190
runMenu();
191+
127192
return 0;
128193
}

0 commit comments

Comments
 (0)