Replace if-else chain with ArgC vector (#625)
This commit is contained in:
parent
286e03f590
commit
370c95c14f
@ -1,6 +1,9 @@
|
||||
#include "command_line.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "io/engine_paths.hpp"
|
||||
#include "util/ArgsReader.hpp"
|
||||
@ -8,45 +11,69 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class ArgC {
|
||||
public:
|
||||
std::string keyword;
|
||||
std::function<bool()> execute;
|
||||
std::string help;
|
||||
ArgC(const std::string& keyword, std::function<bool()> execute, const std::string& help) {
|
||||
this->keyword = keyword;
|
||||
this->execute = execute;
|
||||
this->help = help;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static bool perform_keyword(
|
||||
util::ArgsReader& reader, const std::string& keyword, CoreParameters& params
|
||||
) {
|
||||
if (keyword == "--res") {
|
||||
params.resFolder = reader.next();
|
||||
} else if (keyword == "--dir") {
|
||||
params.userFolder = reader.next();
|
||||
} else if (keyword == "--project") {
|
||||
params.projectFolder = reader.next();
|
||||
} else if (keyword == "--help" || keyword == "-h") {
|
||||
std::cout << "VoxelCore v" << ENGINE_VERSION_STRING << "\n\n";
|
||||
std::cout << "command-line arguments:\n";
|
||||
std::cout << " --help - display this help\n";
|
||||
std::cout << " --version - display engine version\n";
|
||||
std::cout << " --res <path> - set resources directory\n";
|
||||
std::cout << " --dir <path> - set userfiles directory\n";
|
||||
std::cout << " --project <path> - set project directory\n";
|
||||
std::cout << " --headless - run in headless mode\n";
|
||||
std::cout << " --test <path> - test script file\n";
|
||||
std::cout << " --script <path> - main script file\n";
|
||||
std::cout << std::endl;
|
||||
return false;
|
||||
} else if (keyword == "--version") {
|
||||
std::cout << ENGINE_VERSION_STRING << std::endl;
|
||||
return false;
|
||||
} else if (keyword == "--headless") {
|
||||
params.headless = true;
|
||||
} else if (keyword == "--test") {
|
||||
auto token = reader.next();
|
||||
params.testMode = true;
|
||||
params.scriptFile = token;
|
||||
} else if (keyword == "--script") {
|
||||
auto token = reader.next();
|
||||
params.testMode = false;
|
||||
params.scriptFile = token;
|
||||
} else {
|
||||
throw std::runtime_error("unknown argument " + keyword);
|
||||
static const std::vector<ArgC> argumentsCommandline = {
|
||||
ArgC("--res", [¶ms, &reader]() -> bool {
|
||||
params.resFolder = reader.next();
|
||||
return true;
|
||||
}, "<path> - set resources directory."),
|
||||
ArgC("--dir", [¶ms, &reader]() -> bool {
|
||||
params.userFolder = reader.next();
|
||||
return true;
|
||||
}, "<path> - set userfiles directory."),
|
||||
ArgC("--project", [¶ms, &reader]() -> bool {
|
||||
params.projectFolder = reader.next();
|
||||
return true;
|
||||
}, "<path> - set project directory."),
|
||||
ArgC("--test", [¶ms, &reader]() -> bool {
|
||||
params.testMode = true;
|
||||
params.scriptFile = reader.next();
|
||||
return true;
|
||||
}, "<path> - test script file."),
|
||||
ArgC("--script", [¶ms, &reader]() -> bool {
|
||||
params.testMode = false;
|
||||
params.scriptFile = reader.next();
|
||||
return true;
|
||||
}, "<path> - main script file."),
|
||||
ArgC("--headless", [¶ms]() -> bool {
|
||||
params.headless = true;
|
||||
return true;
|
||||
}, "- run in headless mode."),
|
||||
ArgC("--version", []() -> bool {
|
||||
std::cout << ENGINE_VERSION_STRING << std::endl;
|
||||
return false;
|
||||
}, "- display the engine version."),
|
||||
ArgC("--help", []() -> bool {
|
||||
std::cout << "VoxelCore v" << ENGINE_VERSION_STRING << "\n\n";
|
||||
std::cout << "Command-line arguments:\n";
|
||||
for (auto& a : argumentsCommandline) {
|
||||
std::cout << a.keyword << " " << a.help << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return false;
|
||||
}, "- display this help.")
|
||||
};
|
||||
for (auto& a : argumentsCommandline) {
|
||||
if (a.keyword == keyword) {
|
||||
return a.execute();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
throw std::runtime_error("unknown argument " + keyword);
|
||||
}
|
||||
|
||||
bool parse_cmdline(int argc, char** argv, CoreParameters& params) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user