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