juk3n/fluffypublic Fork
⑂ b4bea96
Commits
⬇ Clone ▾
git clone https://git.rickub.com/juk3n/fluffy.git
git clone ssh://git@rickub.com/juk3n/fluffy.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Merge pull request #22 from Juk3n/21-add-handling--version-flagUnverified

21 add handling  version flag
Adrian Duraj committed 2024-07-04T16:57:28+02:00 Browse files
b4bea96 parents: c5330a4 3d19b85
modified app/src/main.cpp +83 -45
@@ -1,10 +1,10 @@
1-#include "ftxui/component/component.hpp"
2-#include "ftxui/component/component_base.hpp"
3-#include "ftxui/component/component_options.hpp"
4-#include "ftxui/component/screen_interactive.hpp"
5-#include "ftxui/dom/elements.hpp"
6-#include "ftxui/screen/screen.hpp"
7-#include "ftxui/screen/string.hpp"
1+#include <ftxui/component/component.hpp>
2+#include <ftxui/component/component_base.hpp>
3+#include <ftxui/component/component_options.hpp>
4+#include <ftxui/component/screen_interactive.hpp>
5+#include <ftxui/dom/elements.hpp>
6+#include <ftxui/screen/screen.hpp>
7+#include <ftxui/screen/string.hpp>
88
99 #include <cstdlib>
1010 #include <filesystem>
@@ -54,46 +54,44 @@ auto runGame(std::string gameName) -> void {
5454 system(pathToRun.c_str());
5555 }
5656
57-auto handleCommand(int argc, char const *argv[], Database& database) -> void{
58- std::string command{};
59- switch (argc) {
60- case 2:
61- command = argv[1];
62- if (command == "show") {
63- for (auto &game : games) {
64- std::cout << game.getName() << ": " << game.getPath() << std::endl;
65- }
57+auto handleCommand(const std::string& command, const std::vector<std::string>& arguments, Database& database) -> void{
58+ if (command == "show") {
59+ for (auto &game : games) {
60+ std::cout << game.getName() << ": " << game.getPath() << std::endl;
6661 }
67- break;
68- case 3:
69- command = argv[1];
70- if (command == "rm") {
71- std::string gameName{argv[2]};
72- removeGame(database, gameName);
73- } else if (command == "play") {
74- std::string gameName{argv[2]};
75- runGame(gameName);
76- }
77- case 4:
78- command = argv[1];
79- if (command == "add") {
80- std::string gameName{argv[2]};
81- auto localPath = std::filesystem::path(argv[3]);
82- std::string globalPath =
83- std::filesystem::absolute(localPath).lexically_normal().string();
84-
85- std::string temp{};
86- for (const auto a : globalPath) {
87- if (a != ' ') {
88- temp += a;
89- } else {
90- temp += "\' \'";
91- }
62+ }
63+ else if (command == "rm") {
64+ std::string gameName{arguments[0]};
65+ removeGame(database, gameName);
66+ }
67+ else if (command == "add") {
68+ std::string gameName{arguments[0]};
69+ auto localPath = std::filesystem::path(arguments[1]);
70+ std::string globalPath =
71+ std::filesystem::absolute(localPath).lexically_normal().string();
72+ std::string temp{};
73+ for (const auto a : globalPath) {
74+ if (a != ' ') {
75+ temp += a;
76+ } else {
77+ temp += "\' \'";
9278 }
93-
94- addGame(database, gameName, temp);
9579 }
96- break;
80+
81+ addGame(database, gameName, temp);
82+ }
83+ else if (command == "play") {
84+ std::string gameName{arguments[0]};
85+ runGame(gameName);
86+ }
87+ else if (command == "--version") {
88+ std::cout << "fluffy v0.1.1" << std::endl;
89+ }
90+ else if (command == "--help") {
91+ std::cout << "help" << std::endl;
92+ }
93+ else {
94+ std::cout << "fluffy: '" << command << "' is not a fluffy command. See 'fluffy --help'" << std::endl;
9795 }
9896 }
9997
@@ -147,12 +145,48 @@ auto runConsoleApp() -> void {
147145
148146 }
149147
148+auto handleFlags(std::vector<std::string> flags) -> void {
149+ for (const auto& flag : flags) {
150+ if (flag == "-test") {
151+ std::cout << "hello!" << std::endl;
152+ }
153+ else {
154+ std::cout << "unkown option: '" << flag << "'" << std::endl;
155+ }
156+ }
157+}
158+
150159 auto getExecutablePath() -> std::filesystem::path {
151160 char result[PATH_MAX];
152161 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
153162 return std::filesystem::path(std::string(result, (count > 0) ? count : 0));
154163 }
155164
165+auto parseFlags(int argc, char const *argv[]) -> std::vector<std::string> {
166+ std::vector<std::string> flags;
167+ for (int i = 1; i < argc; i ++) {
168+ std::string appArgument = argv[i];
169+ if (appArgument.size() < 2) continue;
170+ if (appArgument[0] == '-' and appArgument[1] != '-') flags.emplace_back(appArgument);
171+ }
172+ return flags;
173+}
174+
175+auto parseCommand(int argc, char const* argv[]) {
176+ std::string command{};
177+ std::vector<std::string> arguments{};
178+ for (int i = 1; i < argc; i ++) {
179+ std::string appArgument = argv[i];
180+ if (appArgument.size() < 2) continue;
181+ if ((appArgument[0] == '-' and appArgument[1] == '-') or appArgument[0] != '-') {
182+ if (command == "") command = appArgument;
183+ else arguments.emplace_back(appArgument);
184+ }
185+ }
186+ struct Command { std::string command; std::vector<std::string> arguments; };
187+ return Command{command, arguments};
188+}
189+
156190 auto main(int argc, char const *argv[]) -> int {
157191 try {
158192 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
@@ -160,7 +194,11 @@ auto main(int argc, char const *argv[]) -> int {
160194 games = database.getGames();
161195
162196 if (argc > 1) {
163- handleCommand(argc, argv, database);
197+ auto flags = parseFlags(argc, argv);
198+ auto [command, arguments] = parseCommand(argc, argv);
199+
200+ handleFlags(flags);
201+ if (command != "") handleCommand(command, arguments, database);
164202 }
165203 else {
166204 runConsoleApp();
@@ -1,10 +1,10 @@
1-#include "ftxui/component/component.hpp"1+#include <ftxui/component/component.hpp>
2-#include "ftxui/component/component_base.hpp"2+#include <ftxui/component/component_base.hpp>
3-#include "ftxui/component/component_options.hpp"3+#include <ftxui/component/component_options.hpp>
4-#include "ftxui/component/screen_interactive.hpp"4+#include <ftxui/component/screen_interactive.hpp>
5-#include "ftxui/dom/elements.hpp"5+#include <ftxui/dom/elements.hpp>
6-#include "ftxui/screen/screen.hpp"6+#include <ftxui/screen/screen.hpp>
7-#include "ftxui/screen/string.hpp"7+#include <ftxui/screen/string.hpp>
8 8
9 #include <cstdlib>9 #include <cstdlib>
10 #include <filesystem>10 #include <filesystem>
@@ -54,46 +54,44 @@ auto runGame(std::string gameName) -> void {
54 system(pathToRun.c_str());54 system(pathToRun.c_str());
55 }55 }
56 56
57-auto handleCommand(int argc, char const *argv[], Database& database) -> void{57+auto handleCommand(const std::string& command, const std::vector<std::string>& arguments, Database& database) -> void{
58- std::string command{};58+ if (command == "show") {
59- switch (argc) {59+ for (auto &game : games) {
60- case 2:60+ std::cout << game.getName() << ": " << game.getPath() << std::endl;
61- command = argv[1];
62- if (command == "show") {
63- for (auto &game : games) {
64- std::cout << game.getName() << ": " << game.getPath() << std::endl;
65- }
66 }61 }
67- break;62+ }
68- case 3:63+ else if (command == "rm") {
69- command = argv[1];64+ std::string gameName{arguments[0]};
70- if (command == "rm") {65+ removeGame(database, gameName);
71- std::string gameName{argv[2]};66+ }
72- removeGame(database, gameName);67+ else if (command == "add") {
73- } else if (command == "play") {68+ std::string gameName{arguments[0]};
74- std::string gameName{argv[2]};69+ auto localPath = std::filesystem::path(arguments[1]);
75- runGame(gameName);70+ std::string globalPath =
76- }71+ std::filesystem::absolute(localPath).lexically_normal().string();
77- case 4:72+ std::string temp{};
78- command = argv[1];73+ for (const auto a : globalPath) {
79- if (command == "add") {74+ if (a != ' ') {
80- std::string gameName{argv[2]};75+ temp += a;
81- auto localPath = std::filesystem::path(argv[3]);76+ } else {
82- std::string globalPath =77+ temp += "\' \'";
83- std::filesystem::absolute(localPath).lexically_normal().string();
84-
85- std::string temp{};
86- for (const auto a : globalPath) {
87- if (a != ' ') {
88- temp += a;
89- } else {
90- temp += "\' \'";
91- }
92 }78 }
93-
94- addGame(database, gameName, temp);
95 }79 }
96- break;80+
81+ addGame(database, gameName, temp);
82+ }
83+ else if (command == "play") {
84+ std::string gameName{arguments[0]};
85+ runGame(gameName);
86+ }
87+ else if (command == "--version") {
88+ std::cout << "fluffy v0.1.1" << std::endl;
89+ }
90+ else if (command == "--help") {
91+ std::cout << "help" << std::endl;
92+ }
93+ else {
94+ std::cout << "fluffy: '" << command << "' is not a fluffy command. See 'fluffy --help'" << std::endl;
97 }95 }
98 }96 }
99 97
@@ -147,12 +145,48 @@ auto runConsoleApp() -> void {
147 145
148 }146 }
149 147
148+auto handleFlags(std::vector<std::string> flags) -> void {
149+ for (const auto& flag : flags) {
150+ if (flag == "-test") {
151+ std::cout << "hello!" << std::endl;
152+ }
153+ else {
154+ std::cout << "unkown option: '" << flag << "'" << std::endl;
155+ }
156+ }
157+}
158+
150 auto getExecutablePath() -> std::filesystem::path {159 auto getExecutablePath() -> std::filesystem::path {
151 char result[PATH_MAX];160 char result[PATH_MAX];
152 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);161 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
153 return std::filesystem::path(std::string(result, (count > 0) ? count : 0));162 return std::filesystem::path(std::string(result, (count > 0) ? count : 0));
154 }163 }
155 164
165+auto parseFlags(int argc, char const *argv[]) -> std::vector<std::string> {
166+ std::vector<std::string> flags;
167+ for (int i = 1; i < argc; i ++) {
168+ std::string appArgument = argv[i];
169+ if (appArgument.size() < 2) continue;
170+ if (appArgument[0] == '-' and appArgument[1] != '-') flags.emplace_back(appArgument);
171+ }
172+ return flags;
173+}
174+
175+auto parseCommand(int argc, char const* argv[]) {
176+ std::string command{};
177+ std::vector<std::string> arguments{};
178+ for (int i = 1; i < argc; i ++) {
179+ std::string appArgument = argv[i];
180+ if (appArgument.size() < 2) continue;
181+ if ((appArgument[0] == '-' and appArgument[1] == '-') or appArgument[0] != '-') {
182+ if (command == "") command = appArgument;
183+ else arguments.emplace_back(appArgument);
184+ }
185+ }
186+ struct Command { std::string command; std::vector<std::string> arguments; };
187+ return Command{command, arguments};
188+}
189+
156 auto main(int argc, char const *argv[]) -> int {190 auto main(int argc, char const *argv[]) -> int {
157 try {191 try {
158 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};192 auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")};
@@ -160,7 +194,11 @@ auto main(int argc, char const *argv[]) -> int {
160 games = database.getGames();194 games = database.getGames();
161 195
162 if (argc > 1) {196 if (argc > 1) {
163- handleCommand(argc, argv, database);197+ auto flags = parseFlags(argc, argv);
198+ auto [command, arguments] = parseCommand(argc, argv);
199+
200+ handleFlags(flags);
201+ if (command != "") handleCommand(command, arguments, database);
164 }202 }
165 else {203 else {
166 runConsoleApp();204 runConsoleApp();