command_line refactor
This commit is contained in:
parent
1b1f11e0b8
commit
93acc5ce57
@ -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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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_
|
||||
#endif // UTIL_COMMAND_LINE_H_
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user