dolphin/Source/Core/UICommon/CommandLineParse.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

224 lines
7.9 KiB
C++
Raw Normal View History

2016-01-17 05:11:43 -06:00
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2016-01-17 05:11:43 -06:00
2021-12-09 18:22:16 -08:00
#include "UICommon/CommandLineParse.h"
2016-01-19 08:15:48 -06:00
#include <list>
#include <optional>
2016-01-19 08:15:48 -06:00
#include <sstream>
#include <string>
#include <tuple>
2016-01-17 05:11:43 -06:00
#include <OptionParser.h>
#include <fmt/ostream.h>
2016-01-17 05:11:43 -06:00
2016-01-19 08:15:48 -06:00
#include "Common/Config/Config.h"
2017-10-30 17:15:21 +00:00
#include "Common/StringUtil.h"
#include "Common/Version.h"
2017-10-30 17:15:21 +00:00
#include "Core/Config/MainSettings.h"
#include "Core/Config/NetplaySettings.h"
2016-01-17 05:11:43 -06:00
namespace CommandLineParse
{
2016-01-19 08:15:48 -06:00
class CommandLineConfigLayerLoader final : public Config::ConfigLayerLoader
{
public:
CommandLineConfigLayerLoader(const std::list<std::string>& args, const std::string& video_backend,
const std::string& audio_backend,
const std::optional<u16> netplay_host,
const std::string& netplay_join, bool batch, bool debugger)
2016-01-19 08:15:48 -06:00
: ConfigLayerLoader(Config::LayerType::CommandLine)
{
2019-02-12 23:47:17 +01:00
if (!video_backend.empty())
m_values.emplace_back(Config::MAIN_GFX_BACKEND.GetLocation(), video_backend);
2016-01-19 08:15:48 -06:00
2019-02-12 23:47:17 +01:00
if (!audio_backend.empty())
{
m_values.emplace_back(Config::MAIN_DSP_HLE.GetLocation(),
ValueToString(audio_backend == "HLE"));
}
2016-01-19 08:15:48 -06:00
if (netplay_host.has_value())
{
const std::string traversal_choice = Config::Get(Config::NETPLAY_TRAVERSAL_CHOICE);
const bool is_traversal = traversal_choice == "traversal";
Config::Location config = is_traversal ? Config::NETPLAY_TRAVERSAL_PORT.GetLocation() :
Config::NETPLAY_HOST_PORT.GetLocation();
m_values.emplace_back(config, ValueToString(netplay_host.value()));
}
if (!netplay_join.empty())
{
std::vector<std::string> join_parts = SplitString(netplay_join, ':');
if (join_parts.size() < 2)
{
// The user is submitting a host code
const std::string& host_code = join_parts[0];
m_values.emplace_back(Config::NETPLAY_TRAVERSAL_CHOICE.GetLocation(), "traversal");
m_values.emplace_back(Config::NETPLAY_HOST_CODE.GetLocation(), host_code);
}
else
{
// Ther user is submitting an IP address
const std::string& host = join_parts[0];
const std::string& port_str = join_parts[1];
if (!std::all_of(port_str.begin(), port_str.end(), ::isdigit) || port_str.length() > 5)
{
fmt::println(std::cerr, "Error: the port must be a number between 0-{}.",
std::numeric_limits<uint16_t>::max());
std::exit(EXIT_FAILURE);
}
const u64 port = std::stoul(port_str);
if (port > std::numeric_limits<uint16_t>::max() || port < 1)
{
fmt::println(std::cerr, "Error: the port must be a number between 0-{}.",
std::numeric_limits<uint16_t>::max());
std::exit(EXIT_FAILURE);
}
const u16 cast_port = static_cast<uint16_t>(port);
m_values.emplace_back(Config::NETPLAY_ADDRESS.GetLocation(), host);
m_values.emplace_back(Config::NETPLAY_CONNECT_PORT.GetLocation(), ValueToString(cast_port));
}
}
// Batch mode hides the main window, and render to main hides the render window. To avoid a
// situation where we would have no window at all, disable render to main when using batch mode.
if (batch)
m_values.emplace_back(Config::MAIN_RENDER_TO_MAIN.GetLocation(), ValueToString(false));
if (debugger)
m_values.emplace_back(Config::MAIN_ENABLE_DEBUGGING.GetLocation(), ValueToString(true));
2016-01-19 08:15:48 -06:00
// Arguments are in the format of <System>.<Section>.<Key>=Value
for (const auto& arg : args)
{
std::istringstream buffer(arg);
2017-10-30 17:15:21 +00:00
std::string system_str, section, key, value;
std::getline(buffer, system_str, '.');
2016-01-19 08:15:48 -06:00
std::getline(buffer, section, '.');
std::getline(buffer, key, '=');
std::getline(buffer, value, '=');
2020-08-17 17:23:28 -07:00
std::optional<Config::System> system = Config::GetSystemFromName(system_str);
if (system)
{
m_values.emplace_back(
Config::Location{std::move(*system), std::move(section), std::move(key)},
std::move(value));
}
2016-01-19 08:15:48 -06:00
}
}
void Load(Config::Layer* layer) override
2016-01-19 08:15:48 -06:00
{
for (auto& value : m_values)
{
layer->Set(std::get<0>(value), std::get<1>(value));
2016-01-19 08:15:48 -06:00
}
}
void Save(Config::Layer* layer) override
2016-01-19 08:15:48 -06:00
{
// Save Nothing
}
private:
std::list<std::tuple<Config::Location, std::string>> m_values;
2016-01-19 08:15:48 -06:00
};
2016-01-17 05:11:43 -06:00
std::unique_ptr<optparse::OptionParser> CreateParser(ParserOptions options)
{
auto parser = std::make_unique<optparse::OptionParser>();
parser->usage("usage: %prog [options]... [FILE]...").version(Common::GetScmRevStr());
2016-01-17 05:11:43 -06:00
parser->add_option("-u", "--user").action("store").help("User folder path");
parser->add_option("-m", "--movie").action("store").help("Play a movie file");
parser->add_option("-e", "--exec")
2018-11-05 19:20:45 +01:00
.action("append")
2016-01-17 05:11:43 -06:00
.metavar("<file>")
.type("string")
.help("Load the specified file");
parser->add_option("-n", "--nand_title")
.action("store")
.metavar("<16-character ASCII title ID>")
.type("string")
.help("Launch a NAND title");
2016-01-19 08:15:48 -06:00
parser->add_option("-C", "--config")
.action("append")
.metavar("<System>.<Section>.<Key>=<Value>")
.type("string")
.help("Set a configuration option");
parser->add_option("-s", "--save_state")
.action("store")
.metavar("<file>")
.type("string")
.help("Load the initial save state");
2016-01-17 05:11:43 -06:00
if (options == ParserOptions::IncludeGUIOptions)
{
2017-09-10 01:49:35 -04:00
parser->add_option("-d", "--debugger")
.action("store_true")
.help("Show the debugger pane and additional View menu options");
2017-11-19 16:11:33 -05:00
parser->add_option("-l", "--logger").action("store_true").help("Open the logger");
parser->add_option("-b", "--batch")
.action("store_true")
.help("Run Dolphin without the user interface (Requires --exec or --nand-title)");
2016-01-17 05:11:43 -06:00
parser->add_option("-c", "--confirm").action("store_true").help("Set Confirm on Stop");
parser->add_option("--netplay_host")
.action("store")
.metavar("<port>")
.type("int")
.help("Host a netplay session on the specified port (Requires --exec)");
parser->add_option("--netplay_join")
.action("store")
.metavar("<ip:port> OR <host code>")
.type("string")
.help("Join a netplay session at the specified IP address and port or using a host code");
2016-01-17 05:11:43 -06:00
}
2016-01-19 08:15:48 -06:00
parser->set_defaults("video_backend", "");
parser->set_defaults("audio_emulation", "");
2016-01-17 05:11:43 -06:00
parser->add_option("-v", "--video_backend").action("store").help("Specify a video backend");
parser->add_option("-a", "--audio_emulation")
.choices({"HLE", "LLE"})
.help("Choose audio emulation from [%choices]");
return parser;
}
static void AddConfigLayer(const optparse::Values& options)
2016-01-17 05:11:43 -06:00
{
std::list<std::string> config_args;
if (options.is_set_by_user("config"))
config_args = options.all("config");
Config::AddLayer(std::make_unique<CommandLineConfigLayerLoader>(
std::move(config_args), static_cast<const char*>(options.get("video_backend")),
static_cast<const char*>(options.get("audio_emulation")),
options.is_set("netplay_host") ?
std::optional<u16>(static_cast<u16>(options.get("netplay_host"))) :
std::nullopt,
static_cast<const char*>(options.get("netplay_join")),
static_cast<bool>(options.get("batch")), static_cast<bool>(options.get("debugger"))));
}
optparse::Values& ParseArguments(optparse::OptionParser* parser, int argc, char** argv)
{
optparse::Values& options = parser->parse_args(argc, argv);
AddConfigLayer(options);
return options;
}
optparse::Values& ParseArguments(optparse::OptionParser* parser,
const std::vector<std::string>& arguments)
{
optparse::Values& options = parser->parse_args(arguments);
AddConfigLayer(options);
2016-01-19 08:15:48 -06:00
return options;
2016-01-17 05:11:43 -06:00
}
} // namespace CommandLineParse