Add --working-directory flag

This commit is contained in:
Lion Kortlepel
2021-12-05 00:42:50 +01:00
parent 265dd710cf
commit 7b99ccb08e
+18 -2
View File
@@ -30,6 +30,10 @@ ARGUMENTS:
Server Config file, including the Server Config file, including the
filename. For paths and filenames with filename. For paths and filenames with
spaces, put quotes around the path. spaces, put quotes around the path.
--working-directory=/path/to/folder
Sets the working directory of the Server.
All paths are considered relative to this,
including the path given in --path.
--version --version
Prints version info and exits. Prints version info and exits.
@@ -80,6 +84,7 @@ int BeamMPServerMain(MainArguments Arguments) {
Parser.RegisterArgument({ "help" }, ArgsParser::NONE); Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
Parser.RegisterArgument({ "version" }, ArgsParser::NONE); Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
Parser.RegisterArgument({ "config" }, ArgsParser::HAS_VALUE); Parser.RegisterArgument({ "config" }, ArgsParser::HAS_VALUE);
Parser.RegisterArgument({ "working-directory" }, ArgsParser::HAS_VALUE);
Parser.Parse(Arguments.List); Parser.Parse(Arguments.List);
if (!Parser.Verify()) { if (!Parser.Verify()) {
return 1; return 1;
@@ -94,13 +99,24 @@ int BeamMPServerMain(MainArguments Arguments) {
Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString()); Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString());
return 0; return 0;
} }
std::string ConfigPath = "ServerConfig.toml"; std::string ConfigPath = "ServerConfig.toml";
if (Parser.FoundArgument({ "config" })) { if (Parser.FoundArgument({ "config" })) {
auto MaybeConfigPath = Parser.GetValueOfArgument({ "config" }); auto MaybeConfigPath = Parser.GetValueOfArgument({ "config" });
if (MaybeConfigPath.has_value()) { if (MaybeConfigPath.has_value()) {
ConfigPath = MaybeConfigPath.value(); ConfigPath = MaybeConfigPath.value();
beammp_info("Custom config requested via commandline: '" + ConfigPath + "'"); beammp_info("Custom config requested via commandline arguments: '" + ConfigPath + "'");
}
}
if (Parser.FoundArgument({ "working-directory" })) {
auto MaybeWorkingDirectory = Parser.GetValueOfArgument({ "working-directory" });
if (MaybeWorkingDirectory.has_value()) {
beammp_info("Custom working directory requested via commandline arguments: '" + ConfigPath + "'");
try {
fs::current_path(fs::path(MaybeWorkingDirectory.value()));
} catch (const std::exception& e) {
beammp_error("Could not set working directory to '" + MaybeWorkingDirectory.value() + "': " + e.what());
}
} }
} }