diff --git a/src/util/command_line.cpp b/src/util/command_line.cpp index 097b58c3..8392a0c1 100644 --- a/src/util/command_line.cpp +++ b/src/util/command_line.cpp @@ -4,37 +4,46 @@ 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) { - ArgsReader reader(argc, argv); - reader.skip(); - while (reader.hasNext()) { - std::string token = reader.next(); - if (reader.isKeywordArg()) { - if (token == "--res") { - 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 (token == "--dir") { - 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 (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; -} \ No newline at end of file + ArgsReader reader(argc, argv); + reader.skip(); + while (reader.hasNext()) { + std::string token = reader.next(); + if (reader.isKeywordArg()) { + if (!perform_keyword(reader, token, paths)) { + return false; + } + } else { + std::cerr << "unexpected token" << std::endl; + return false; + } + } + return true; +} diff --git a/src/util/command_line.h b/src/util/command_line.h index a1b6cd49..11441b6d 100644 --- a/src/util/command_line.h +++ b/src/util/command_line.h @@ -7,35 +7,35 @@ #include "../files/engine_paths.h" class ArgsReader { - int argc; - char** argv; - int pos = 0; - const char* last = ""; + int argc; + char** argv; + int pos = 0; + const char* last = ""; public: - ArgsReader(int argc, char** argv) : argc(argc), argv(argv) {} + ArgsReader(int argc, char** argv) : argc(argc), argv(argv) {} - void skip() { - pos++; - } + void skip() { + pos++; + } - bool hasNext() const { - return pos < argc; - } + bool hasNext() const { + return pos < argc; + } - bool isKeywordArg() const { - return last[0] == '-'; - } + bool isKeywordArg() const { + return last[0] == '-'; + } - std::string next() { - if (pos >= argc) { - throw std::runtime_error("unexpected end"); - } - last = argv[pos]; - return argv[pos++]; - } + std::string next() { + if (pos >= argc) { + throw std::runtime_error("unexpected end"); + } + last = argv[pos]; + return argv[pos++]; + } }; /* @return false if engine start can*/ extern bool parse_cmdline(int argc, char** argv, EnginePaths& paths); -#endif // UTIL_COMMAND_LINE_H_ \ No newline at end of file +#endif // UTIL_COMMAND_LINE_H_