Skip to content

Commit 6e20161

Browse files
committed
Better Arg Handleing
1 parent 1986ab9 commit 6e20161

File tree

4 files changed

+126
-56
lines changed

4 files changed

+126
-56
lines changed

src/ArgCheck.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "ArgCheck.hpp"
2+
#include "RobloxData/GetRobloxData.hpp"
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <cctype>
7+
#include <stdexcept>
8+
#include <algorithm>
9+
10+
#ifdef WIN32
11+
#include <raylib_win32.h>
12+
#include <windows.h>
13+
#endif
14+
15+
bool isNumber(const std::string& s) {
16+
return !s.empty() && std::all_of(s.begin(), s.end(), [](unsigned char c) {
17+
return std::isdigit(c) || c == '.' || c == '-';
18+
});
19+
}
20+
21+
bool hasNextArg(int i, int argc) {
22+
return (i + 1 < argc);
23+
}
24+
25+
26+
bool ArgCheck::Check(int argc, char **argv) {
27+
for (int i = 1; i < argc; i++) {
28+
std::string arg = argv[i];
29+
30+
if ((arg == "--universeid" || arg == "--unid") && hasNextArg(i, argc)) {
31+
universeId = argv[++i];
32+
if (!isNumber(universeId)) {
33+
std::cerr << "Error: Invalid universe ID: " << universeId << "\n";
34+
return NOTVALID;
35+
}
36+
}
37+
else if (arg == "--nogui") {
38+
UseGui = false;
39+
}
40+
else if (arg == "--nofont") {
41+
UseFont = false;
42+
}
43+
else if (arg == "--reftime" && hasNextArg(i, argc)) {
44+
std::string val = argv[++i];
45+
try {
46+
RefTime = std::stof(val);
47+
} catch (...) {
48+
std::cerr << "Error: Invalid value for --reftime: " << val << "\n";
49+
return NOTVALID;
50+
}
51+
}
52+
else if (arg == "--url2uni" && hasNextArg(i, argc)) {
53+
uint64_t Unid = 0;
54+
std::string url = argv[++i];
55+
GetUniverseID(Unid, Str2U64(StripURL(url)));
56+
std::cout << "UniverseID: " << Unid << "\n";
57+
return NOTVALID;
58+
}
59+
else if (arg == "--url" && hasNextArg(i, argc)) {
60+
uint64_t Unid = 0;
61+
std::string url = argv[++i];
62+
GetUniverseID(Unid, Str2U64(StripURL(url)));
63+
universeId = std::to_string(Unid);
64+
}
65+
else if (arg == "--placeid" && hasNextArg(i, argc)) {
66+
uint64_t Unid = 0;
67+
std::string place = argv[++i];
68+
if (!isNumber(place)) {
69+
std::cerr << "Error: Invalid place ID: " << place << "\n";
70+
return NOTVALID;
71+
}
72+
GetUniverseID(Unid, Str2U64(place));
73+
universeId = std::to_string(Unid);
74+
}
75+
else if (arg == "--noterm") {
76+
#ifdef WIN32
77+
HWND hwnd = GetConsoleWindow();
78+
ShowWindow(hwnd, SW_HIDE);
79+
#endif
80+
}
81+
else if (arg == "--help") {
82+
std::cout << "--universeid / --unid # Set Universe ID\n"
83+
<< "--nogui # Uses Terminal As Output\n"
84+
<< "--nofont # Disables font in GUI mode\n"
85+
<< "--noterm # Windows Only, hides terminal\n"
86+
<< "--url2uni # Converts Roblox Link To Universe ID\n"
87+
<< "--url # Converts Link To Universe ID and Starts RStats\n"
88+
<< "--placeid # Converts PlaceID to UniverseID and Starts RStats\n"
89+
<< "--reftime # Example: --reftime 5\n";
90+
return NOTVALID;
91+
}
92+
else {
93+
std::cerr << "Invalid argument: " << arg << "\n";
94+
return NOTVALID;
95+
}
96+
}
97+
return ALLVALID;
98+
}

src/ArgCheck.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <string>
3+
4+
#define ALLVALID true
5+
#define NOTVALID false
6+
7+
class ArgCheck {
8+
public:
9+
std::string universeId = "8353903143";
10+
bool UseGui = true;
11+
bool UseFont = true;
12+
float RefTime = 5;
13+
14+
bool Check(int argc, char **argv);
15+
};

src/Graphics/Core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Graphics::Graphics(uint64_t UniverseID, float RefreshTime,bool UseFont) : UseFon
1212

1313
SetTargetFPS(30);
1414

15-
if(UseFont) GlobalFont = LoadFontEx("Font.ttf", 128, nullptr, 0);
15+
if(UseFont) GlobalFont = LoadFontEx("Font.ttf", 64, nullptr, 0);
1616
else TextScale = 0.9;
1717

1818
Stats.Update();

src/main.cpp

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,27 @@
66
#include "RobloxData/GetRobloxData.hpp"
77
#include "TerminalGraphics/NoGui.hpp"
88
#include "Graphics/Graphics.hpp"
9+
#include "ArgCheck.hpp"
910

1011
#include <iostream>
1112

12-
int main(int argc, char **argv) {
13-
std::string universeId = "8353903143";
14-
bool UseGui = true;
15-
bool UseFont = true;
16-
float RefTime = 5;
13+
std::string Bool2Str(bool Bool) {return Bool ? "True" : "False";}
1714

18-
for (int i = 1; i < argc; i++) {
19-
std::string arg = argv[i];
20-
if (arg == "--universeid" && i + 1 < argc) {
21-
universeId = argv[++i];
22-
} else if (arg == "--unid" && i + 1 < argc) {
23-
universeId = argv[++i];
24-
} else if (arg == "--nogui") {
25-
UseGui = false;
26-
} else if (arg == "--nofont") {
27-
UseFont = false;
28-
} else if (arg == "--reftime") {
29-
RefTime = std::stof(argv[++i]);
30-
} else if (arg == "--url2uni") {
31-
uint64_t Unid = 0;
32-
GetUniverseID(Unid, Str2U64(StripURL(std::string(argv[++i]))));
33-
std::cout << "UniverseID: " << Unid << "\n";
34-
return 0;
35-
} else if (arg == "--url") {
36-
uint64_t Unid = 0;
37-
GetUniverseID(Unid, Str2U64(StripURL(std::string(argv[++i]))));
38-
universeId = std::to_string(Unid);
39-
} else if (arg == "--placeid") {
40-
uint64_t Unid = 0;
41-
GetUniverseID(Unid, Str2U64(std::string(argv[++i])));
42-
universeId = std::to_string(Unid);
43-
} else if (arg == "--noterm") {
44-
#ifdef WIN32
45-
HWND hwnd = GetConsoleWindow();
46-
ShowWindow(hwnd, SW_HIDE);
47-
#endif
48-
} else if (arg == "--help") {
49-
std::cout << "--universeid / --unid # Use To Set Universe ID\n"
50-
<< "--nogui # Uses Terminal As Output (def refresh 1 sec, clears screen)\n"
51-
<< "--nofont # Makes Gui Mode Not Load Font\n"
52-
<< "--noterm # Windows Only!!! Hides Terminal \n"
53-
<< "--url2uni # Converts Roblox Link To Universe ID\n"
54-
<< "--url # Converts Link To Universe ID and Starts RStats With it\n"
55-
<< "--placeid # Converts PlaceID to UniverseID Adn Starts RStats With it\n"
56-
<< "--reftime # ex --reftime 5 # Sets Refresh Time if 0 Program Will not refresh\n";
57-
return 0;
58-
} else {
59-
std::cout << "Invalid Arg -> " << arg << "\n" << "Quiting!!!\n";
60-
return 0;
61-
}
62-
}
15+
int main(int argc, char **argv) {
16+
ArgCheck Arg;
17+
if(Arg.Check(argc, argv) == NOTVALID) return 0;
6318

64-
std::cout << "Universe ID: " << universeId << "\n";
65-
std::cout << "Gui: " << UseGui << "\n";
19+
std::cout << "Universe ID: " << Arg.universeId << "\n";
20+
std::cout << "Refreah Time: " << Arg.RefTime << " if = 0 No Refresh\n";
21+
std::cout << "Use Font: " << Bool2Str(Arg.UseFont) << "\n";
22+
std::cout << "Gui: " << Bool2Str(Arg.UseGui) << "\n";
6623

67-
if(UseGui) {
24+
if(Arg.UseGui) {
6825
std::cout << "Graphics Api: Raylib\n";
69-
Graphics Raylib(Str2U64(universeId), RefTime, UseFont);
26+
Graphics Raylib(Str2U64(Arg.universeId), Arg.RefTime, Arg.UseFont);
7027
Raylib.Loop();
7128
} else {
72-
NoGui Tui(Str2U64(universeId), RefTime);
29+
NoGui Tui(Str2U64(Arg.universeId), Arg.RefTime);
7330
Tui.Loop();
7431
}
7532
return 0;

0 commit comments

Comments
 (0)