21 add handling version flag #22
Merged@github-juk3n wants to merge
21-add-handling--version-flag into mainMerged as
b4bea96ba0f5.1 file changed
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> | |
| 8 | 8 | |
| 9 | 9 | #include <cstdlib> |
| 10 | 10 | #include <filesystem> |
| @@ -54,46 +54,44 @@ auto runGame(std::string gameName) -> void { | ||
| 54 | 54 | system(pathToRun.c_str()); |
| 55 | 55 | } |
| 56 | 56 | |
| 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; | |
| 66 | 61 | } |
| 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 += "\' \'"; | |
| 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 | 159 | auto getExecutablePath() -> std::filesystem::path { |
| 151 | 160 | char result[PATH_MAX]; |
| 152 | 161 | ssize_t count = readlink("/proc/self/exe", result, PATH_MAX); |
| 153 | 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 | 190 | auto main(int argc, char const *argv[]) -> int { |
| 157 | 191 | try { |
| 158 | 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 | 194 | games = database.getGames(); |
| 161 | 195 | |
| 162 | 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 | 203 | else { |
| 166 | 204 | 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(); |