added parsing data from app arguments
6b30a5c parent: c5330a4 modified
app/src/main.cpp +60 -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,35 @@ 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 | - } | |
| 66 | - } | |
| 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); | |
| 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; | |
| 76 | 61 | } |
| 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); | |
| 97 | 86 | } |
| 98 | 87 | } |
| 99 | 88 | |
| @@ -153,6 +142,29 @@ auto getExecutablePath() -> std::filesystem::path { | ||
| 153 | 142 | return std::filesystem::path(std::string(result, (count > 0) ? count : 0)); |
| 154 | 143 | } |
| 155 | 144 | |
| 145 | +auto parseFlags(int argc, char const *argv[]) -> std::vector<std::string> { | |
| 146 | + std::vector<std::string> flags; | |
| 147 | + for (int i = 1; i < argc; i ++) { | |
| 148 | + std::string appArgument = argv[i]; | |
| 149 | + if (appArgument[0] == '-') flags.emplace_back(appArgument); | |
| 150 | + } | |
| 151 | + return flags; | |
| 152 | +} | |
| 153 | + | |
| 154 | +auto parseCommand(int argc, char const* argv[]) { | |
| 155 | + std::string command{}; | |
| 156 | + std::vector<std::string> arguments{}; | |
| 157 | + for (int i = 1; i < argc; i ++) { | |
| 158 | + std::string appArgument = argv[i]; | |
| 159 | + if (appArgument[0] != '-') { | |
| 160 | + if (command == "") command = appArgument; | |
| 161 | + else arguments.emplace_back(appArgument); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + struct Command { std::string command; std::vector<std::string> arguments; }; | |
| 165 | + return Command{command, arguments}; | |
| 166 | +} | |
| 167 | + | |
| 156 | 168 | auto main(int argc, char const *argv[]) -> int { |
| 157 | 169 | try { |
| 158 | 170 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; |
| @@ -160,7 +172,10 @@ auto main(int argc, char const *argv[]) -> int { | ||
| 160 | 172 | games = database.getGames(); |
| 161 | 173 | |
| 162 | 174 | if (argc > 1) { |
| 163 | - handleCommand(argc, argv, database); | |
| 175 | + auto flags = parseFlags(argc, argv); | |
| 176 | + auto [command, arguments] = parseCommand(argc, argv); | |
| 177 | + | |
| 178 | + handleCommand(command, arguments, database); | |
| 164 | 179 | } |
| 165 | 180 | else { |
| 166 | 181 | 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,35 @@ 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 | - } | ||
| 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 | } | 61 | } |
| 77 | - case 4: | 62 | + } |
| 78 | - command = argv[1]; | 63 | + else if (command == "rm") { |
| 79 | - if (command == "add") { | 64 | + std::string gameName{arguments[0]}; |
| 80 | - std::string gameName{argv[2]}; | 65 | + removeGame(database, gameName); |
| 81 | - auto localPath = std::filesystem::path(argv[3]); | 66 | + } |
| 82 | - std::string globalPath = | 67 | + else if (command == "add") { |
| 83 | - std::filesystem::absolute(localPath).lexically_normal().string(); | 68 | + std::string gameName{arguments[0]}; |
| 84 | - | 69 | + auto localPath = std::filesystem::path(arguments[1]); |
| 85 | - std::string temp{}; | 70 | + std::string globalPath = |
| 86 | - for (const auto a : globalPath) { | 71 | + std::filesystem::absolute(localPath).lexically_normal().string(); |
| 87 | - if (a != ' ') { | 72 | + std::string temp{}; |
| 88 | - temp += a; | 73 | + for (const auto a : globalPath) { |
| 89 | - } else { | 74 | + if (a != ' ') { |
| 90 | - temp += "\' \'"; | 75 | + temp += a; |
| 91 | - } | 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); | ||
| 97 | } | 86 | } |
| 98 | } | 87 | } |
| 99 | 88 | ||
| @@ -153,6 +142,29 @@ auto getExecutablePath() -> std::filesystem::path { | |||
| 153 | return std::filesystem::path(std::string(result, (count > 0) ? count : 0)); | 142 | return std::filesystem::path(std::string(result, (count > 0) ? count : 0)); |
| 154 | } | 143 | } |
| 155 | 144 | ||
| 145 | +auto parseFlags(int argc, char const *argv[]) -> std::vector<std::string> { | ||
| 146 | + std::vector<std::string> flags; | ||
| 147 | + for (int i = 1; i < argc; i ++) { | ||
| 148 | + std::string appArgument = argv[i]; | ||
| 149 | + if (appArgument[0] == '-') flags.emplace_back(appArgument); | ||
| 150 | + } | ||
| 151 | + return flags; | ||
| 152 | +} | ||
| 153 | + | ||
| 154 | +auto parseCommand(int argc, char const* argv[]) { | ||
| 155 | + std::string command{}; | ||
| 156 | + std::vector<std::string> arguments{}; | ||
| 157 | + for (int i = 1; i < argc; i ++) { | ||
| 158 | + std::string appArgument = argv[i]; | ||
| 159 | + if (appArgument[0] != '-') { | ||
| 160 | + if (command == "") command = appArgument; | ||
| 161 | + else arguments.emplace_back(appArgument); | ||
| 162 | + } | ||
| 163 | + } | ||
| 164 | + struct Command { std::string command; std::vector<std::string> arguments; }; | ||
| 165 | + return Command{command, arguments}; | ||
| 166 | +} | ||
| 167 | + | ||
| 156 | auto main(int argc, char const *argv[]) -> int { | 168 | auto main(int argc, char const *argv[]) -> int { |
| 157 | try { | 169 | try { |
| 158 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; | 170 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; |
| @@ -160,7 +172,10 @@ auto main(int argc, char const *argv[]) -> int { | |||
| 160 | games = database.getGames(); | 172 | games = database.getGames(); |
| 161 | 173 | ||
| 162 | if (argc > 1) { | 174 | if (argc > 1) { |
| 163 | - handleCommand(argc, argv, database); | 175 | + auto flags = parseFlags(argc, argv); |
| 176 | + auto [command, arguments] = parseCommand(argc, argv); | ||
| 177 | + | ||
| 178 | + handleCommand(command, arguments, database); | ||
| 164 | } | 179 | } |
| 165 | else { | 180 | else { |
| 166 | runConsoleApp(); | 181 | runConsoleApp(); |