command_line refactor

This commit is contained in:
MihailRis
2024-03-20 09:17:04 +03:00
parent 1b1f11e0b8
commit 93acc5ce57
2 changed files with 64 additions and 55 deletions
+41 -32
View File
@@ -4,37 +4,46 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
bool perform_keyword(ArgsReader& reader, const std::string& keyword, EnginePaths& paths) {
if (keyword == "--res") {
auto token = reader.next();
if (!fs::is_directory(fs::path(token))) {
throw std::runtime_error(token+" is not a directory");
}
paths.setResources(fs::path(token));
std::cout << "resources folder: " << token << std::endl;
} else if (keyword == "--dir") {
auto token = reader.next();
if (!fs::is_directory(fs::path(token))) {
fs::create_directories(fs::path(token));
}
paths.setUserfiles(fs::path(token));
std::cout << "userfiles folder: " << token << std::endl;
} else if (keyword == "--help" || keyword == "-h") {
std::cout << "VoxelEngine command-line arguments:" << std::endl;
std::cout << " --res [path] - set resources directory" << std::endl;
std::cout << " --dir [path] - set userfiles directory" << std::endl;
return false;
} else {
std::cerr << "unknown argument " << keyword << std::endl;
return false;
}
return true;
}
bool parse_cmdline(int argc, char** argv, EnginePaths& paths) { bool parse_cmdline(int argc, char** argv, EnginePaths& paths) {
ArgsReader reader(argc, argv); ArgsReader reader(argc, argv);
reader.skip(); reader.skip();
while (reader.hasNext()) { while (reader.hasNext()) {
std::string token = reader.next(); std::string token = reader.next();
if (reader.isKeywordArg()) { if (reader.isKeywordArg()) {
if (token == "--res") { if (!perform_keyword(reader, token, paths)) {
token = reader.next(); return false;
if (!fs::is_directory(fs::path(token))) { }
throw std::runtime_error(token+" is not a directory"); } else {
} std::cerr << "unexpected token" << std::endl;
paths.setResources(fs::path(token)); return false;
std::cout << "resources folder: " << token << std::endl; }
} else if (token == "--dir") { }
token = reader.next(); return true;
if (!fs::is_directory(fs::path(token))) {
fs::create_directories(fs::path(token));
}
paths.setUserfiles(fs::path(token));
std::cout << "userfiles folder: " << token << std::endl;
} else if (token == "--help" || token == "-h") {
std::cout << "VoxelEngine command-line arguments:" << std::endl;
std::cout << " --res [path] - set resources directory" << std::endl;
std::cout << " --dir [path] - set userfiles directory" << std::endl;
return false;
} else {
std::cerr << "unknown argument " << token << std::endl;
}
} else {
std::cerr << "unexpected token" << std::endl;
}
}
return true;
} }
+21 -21
View File
@@ -7,32 +7,32 @@
#include "../files/engine_paths.h" #include "../files/engine_paths.h"
class ArgsReader { class ArgsReader {
int argc; int argc;
char** argv; char** argv;
int pos = 0; int pos = 0;
const char* last = ""; const char* last = "";
public: public:
ArgsReader(int argc, char** argv) : argc(argc), argv(argv) {} ArgsReader(int argc, char** argv) : argc(argc), argv(argv) {}
void skip() { void skip() {
pos++; pos++;
} }
bool hasNext() const { bool hasNext() const {
return pos < argc; return pos < argc;
} }
bool isKeywordArg() const { bool isKeywordArg() const {
return last[0] == '-'; return last[0] == '-';
} }
std::string next() { std::string next() {
if (pos >= argc) { if (pos >= argc) {
throw std::runtime_error("unexpected end"); throw std::runtime_error("unexpected end");
} }
last = argv[pos]; last = argv[pos];
return argv[pos++]; return argv[pos++];
} }
}; };
/* @return false if engine start can*/ /* @return false if engine start can*/