Merge pull request #4916 from leoetlino/cpp-optparse

Use cpp-optparse for command line parsing
This commit is contained in:
Anthony
2017-02-26 15:51:35 -08:00
committed by GitHub
16 changed files with 1104 additions and 89 deletions

View File

@ -91,7 +91,7 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(WXLIBS ${WXLIBS} dl)
endif()
list(APPEND LIBS core uicommon)
list(APPEND LIBS core uicommon cpp-optparse)
if(APPLE)
if(wxWidgets_FOUND)

View File

@ -48,8 +48,9 @@
<ResourceCompile>
<AdditionalIncludeDirectories>$(ExternalsDir)wxWidgets3\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
<ClCompile />
<ClCompile />
<ClCompile>
<AdditionalIncludeDirectories>$(ExternalsDir)cpp-optparse;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Manifest>
<AdditionalManifestFiles>DolphinWX.manifest;%(AdditionalManifestFiles)</AdditionalManifestFiles>
</Manifest>
@ -228,6 +229,9 @@
<ProjectReference Include="$(ExternalsDir)Bochs_disasm\Bochs_disasm.vcxproj">
<Project>{8ada04d7-6db1-4da4-ab55-64fb12a0997b}</Project>
</ProjectReference>
<ProjectReference Include="$(ExternalsDir)cpp-optparse\cpp-optparse.vcxproj">
<Project>{C636D9D1-82FE-42B5-9987-63B7D4836341}</Project>
</ProjectReference>
<ProjectReference Include="$(ExternalsDir)libpng\png\png.vcxproj">
<Project>{4c9f135b-a85e-430c-bad4-4c67ef5fc12c}</Project>
</ProjectReference>

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <OptionParser.h>
#include <cstdio>
#include <cstring>
#include <mutex>
@ -47,6 +48,7 @@
#include "DolphinWX/VideoConfigDiag.h"
#include "DolphinWX/WxUtils.h"
#include "UICommon/CommandLineParse.h"
#include "UICommon/UICommon.h"
#include "VideoCommon/VideoBackendBase.h"
@ -77,6 +79,11 @@ bool DolphinApp::Initialize(int& c, wxChar** v)
// The 'main program' equivalent that creates the main window and return the main frame
void DolphinApp::OnInitCmdLine(wxCmdLineParser& parser)
{
parser.SetCmdLine("");
}
bool DolphinApp::OnInit()
{
std::lock_guard<std::mutex> lk(s_init_mutex);
@ -96,6 +103,8 @@ bool DolphinApp::OnInit()
wxHandleFatalExceptions(true);
#endif
ParseCommandLine();
UICommon::SetUserDirectory(m_user_path.ToStdString());
UICommon::CreateDirectories();
InitLanguageSupport(); // The language setting is loaded from the user directory
@ -130,60 +139,41 @@ bool DolphinApp::OnInit()
return true;
}
void DolphinApp::OnInitCmdLine(wxCmdLineParser& parser)
void DolphinApp::ParseCommandLine()
{
static const wxCmdLineEntryDesc desc[] = {
{wxCMD_LINE_SWITCH, "h", "help", "Show this help message", wxCMD_LINE_VAL_NONE,
wxCMD_LINE_OPTION_HELP},
{wxCMD_LINE_SWITCH, "d", "debugger", "Opens the debugger", wxCMD_LINE_VAL_NONE,
wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_SWITCH, "l", "logger", "Opens the logger", wxCMD_LINE_VAL_NONE,
wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_OPTION, "e", "exec",
"Loads the specified file (ELF, DOL, GCM, ISO, TGC, WBFS, CISO, GCZ, WAD)",
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_SWITCH, "b", "batch", "Exit Dolphin with emulator", wxCMD_LINE_VAL_NONE,
wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_OPTION, "c", "confirm", "Set Confirm on Stop", wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_OPTION, "v", "video_backend", "Specify a video backend", wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_OPTION, "a", "audio_emulation", "Low level (LLE) or high level (HLE) audio",
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_OPTION, "m", "movie", "Play a movie file", wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_OPTION, "u", "user", "User folder path", wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL},
{wxCMD_LINE_NONE, nullptr, nullptr, nullptr, wxCMD_LINE_VAL_NONE, 0}};
auto parser = CommandLineParse::CreateParser(CommandLineParse::ParserOptions::IncludeGUIOptions);
optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv);
std::vector<std::string> args = parser->args();
parser.SetDesc(desc);
}
bool DolphinApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
if (argc == 2 && File::Exists(argv[1].ToUTF8().data()))
if (options.is_set("exec"))
{
m_load_file = true;
m_file_to_load = argv[1];
m_file_to_load = static_cast<const char*>(options.get("exec"));
}
else if (parser.Parse() != 0)
else if (args.size())
{
return false;
m_load_file = true;
m_file_to_load = args.front();
args.erase(args.begin());
}
if (!m_load_file)
m_load_file = parser.Found("exec", &m_file_to_load);
m_use_debugger = options.is_set("debugger");
m_use_logger = options.is_set("logger");
m_batch_mode = options.is_set("batch");
m_use_debugger = parser.Found("debugger");
m_use_logger = parser.Found("logger");
m_batch_mode = parser.Found("batch");
m_confirm_stop = parser.Found("confirm", &m_confirm_setting);
m_select_video_backend = parser.Found("video_backend", &m_video_backend_name);
m_select_audio_emulation = parser.Found("audio_emulation", &m_audio_emulation_name);
m_play_movie = parser.Found("movie", &m_movie_file);
parser.Found("user", &m_user_path);
m_confirm_stop = options.is_set("confirm");
m_confirm_setting = options.get("confirm");
return true;
m_select_video_backend = options.is_set("video_backend");
m_video_backend_name = static_cast<const char*>(options.get("video_backend"));
m_select_audio_emulation = options.is_set("audio_emulation");
m_audio_emulation_name = static_cast<const char*>(options.get("audio_emulation"));
m_play_movie = options.is_set("movie");
m_movie_file = static_cast<const char*>(options.get("movie"));
m_user_path = static_cast<const char*>(options.get("user"));
}
#ifdef __APPLE__
@ -224,12 +214,7 @@ void DolphinApp::AfterInit()
}
if (m_confirm_stop)
{
if (m_confirm_setting.Upper() == "TRUE")
SConfig::GetInstance().bConfirmStop = true;
else if (m_confirm_setting.Upper() == "FALSE")
SConfig::GetInstance().bConfirmStop = false;
}
SConfig::GetInstance().bConfirmStop = m_confirm_setting;
if (m_play_movie && !m_movie_file.empty())
{

View File

@ -8,6 +8,7 @@
#include <wx/app.h>
class CFrame;
class wxCmdLineParser;
class wxLocale;
extern CFrame* main_frame;
@ -21,9 +22,8 @@ public:
private:
bool OnInit() override;
int OnExit() override;
void OnInitCmdLine(wxCmdLineParser& parser) override;
bool OnCmdLineParsed(wxCmdLineParser& parser) override;
int OnExit() override;
void OnFatalException() override;
bool Initialize(int& c, wxChar** v) override;
@ -37,6 +37,8 @@ private:
void OnActivate(wxActivateEvent& ev);
void OnIdle(wxIdleEvent&);
void ParseCommandLine();
bool m_batch_mode = false;
bool m_confirm_stop = false;
bool m_is_active = true;
@ -46,7 +48,7 @@ private:
bool m_use_logger = false;
bool m_select_video_backend = false;
bool m_select_audio_emulation = false;
wxString m_confirm_setting;
bool m_confirm_setting;
wxString m_video_backend_name;
wxString m_audio_emulation_name;
wxString m_user_path;

View File

@ -2,10 +2,10 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <OptionParser.h>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <getopt.h>
#include <signal.h>
#include <string>
#include <thread>
@ -29,6 +29,7 @@
#include "Core/IOS/USB/Bluetooth/WiimoteDevice.h"
#include "Core/State.h"
#include "UICommon/CommandLineParse.h"
#include "UICommon/UICommon.h"
#include "VideoCommon/RenderBase.h"
@ -379,37 +380,24 @@ static Platform* GetPlatform()
int main(int argc, char* argv[])
{
int ch, help = 0;
struct option longopts[] = {{"exec", no_argument, nullptr, 'e'},
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, nullptr, 'v'},
{nullptr, 0, nullptr, 0}};
std::string boot_filename;
auto parser = CommandLineParse::CreateParser(CommandLineParse::ParserOptions::OmitGUIOptions);
optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv);
std::vector<std::string> args = parser->args();
while ((ch = getopt_long(argc, argv, "eh?v", longopts, 0)) != -1)
if (options.is_set("exec"))
{
switch (ch)
{
case 'e':
break;
case 'h':
case '?':
help = 1;
break;
case 'v':
fprintf(stderr, "%s\n", scm_rev_str.c_str());
return 1;
}
boot_filename = static_cast<const char*>(options.get("exec"));
}
if (help == 1 || argc == optind)
else if (args.size())
{
fprintf(stderr, "%s\n\n", scm_rev_str.c_str());
fprintf(stderr, "A multi-platform GameCube/Wii emulator\n\n");
fprintf(stderr, "Usage: %s [-e <file>] [-h] [-v]\n", argv[0]);
fprintf(stderr, " -e, --exec Load the specified file\n");
fprintf(stderr, " -h, --help Show this help message\n");
fprintf(stderr, " -v, --version Print version and exit\n");
return 1;
boot_filename = args.front();
args.erase(args.begin());
}
else
{
parser->print_help();
return 0;
}
platform = GetPlatform();
@ -435,9 +423,9 @@ int main(int argc, char* argv[])
DolphinAnalytics::Instance()->ReportDolphinStart("nogui");
if (!BootManager::BootCore(argv[optind]))
if (!BootManager::BootCore(boot_filename))
{
fprintf(stderr, "Could not boot %s\n", argv[optind]);
fprintf(stderr, "Could not boot %s\n", boot_filename.c_str());
return 1;
}

View File

@ -1,8 +1,9 @@
set(SRCS Disassembler.cpp
set(SRCS CommandLineParse.cpp
Disassembler.cpp
UICommon.cpp
USBUtils.cpp)
set(LIBS common)
set(LIBS common cpp-optparse)
if(LIBUSB_FOUND)
set(LIBS ${LIBS} ${LIBUSB_LIBRARIES})
endif()

View File

@ -0,0 +1,48 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <OptionParser.h>
#include "Common/Common.h"
#include "UICommon/CommandLineParse.h"
namespace CommandLineParse
{
std::unique_ptr<optparse::OptionParser> CreateParser(ParserOptions options)
{
auto parser = std::make_unique<optparse::OptionParser>();
parser->usage("usage: %prog [options]... [FILE]...").version(scm_rev_str);
parser->add_option("--version").action("version").help("Print version and exit");
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")
.action("store")
.metavar("<file>")
.type("string")
.help("Load the specified file");
if (options == ParserOptions::IncludeGUIOptions)
{
parser->add_option("-d", "--debugger").action("store_true").help("Opens the debuger");
parser->add_option("-l", "--logger").action("store_true").help("Opens the logger");
parser->add_option("-b", "--batch").action("store_true").help("Exit Dolphin with emulation");
parser->add_option("-c", "--confirm").action("store_true").help("Set Confirm on Stop");
}
// XXX: These two are setting configuration options
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;
}
optparse::Values& ParseArguments(optparse::OptionParser* parser, int argc, char** argv)
{
return parser->parse_args(argc, argv);
}
}

View File

@ -0,0 +1,23 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <memory>
namespace optparse
{
class OptionParser;
class Values;
}
namespace CommandLineParse
{
enum class ParserOptions
{
IncludeGUIOptions,
OmitGUIOptions,
};
std::unique_ptr<optparse::OptionParser> CreateParser(ParserOptions options);
optparse::Values& ParseArguments(optparse::OptionParser* parser, int argc, char** argv);
}

View File

@ -34,6 +34,16 @@
<Import Project="..\..\VSProps\PCHUse.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<AdditionalIncludeDirectories>$(ExternalsDir)zlib;$(ExternalsDir)cpp-optparse;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>$(ExternalsDir)zlib;$(ExternalsDir)cpp-optparse;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
@ -43,6 +53,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CommandLineParse.cpp" />
<ClCompile Include="UICommon.cpp" />
<ClCompile Include="Disassembler.cpp" />
<ClCompile Include="USBUtils.cpp">
@ -50,11 +61,17 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CommandLineParse.h" />
<ClInclude Include="UICommon.h" />
<ClInclude Include="Disassembler.h" />
<ClInclude Include="USBUtils.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(ExternalsDir)cpp-optparse\cpp-optparse.vcxproj">
<Project>{C636D9D1-82FE-42B5-9987-63B7D4836341}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -80,6 +80,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DolphinQt2", "Core\DolphinQ
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "glslang", "..\Externals\glslang\glslang.vcxproj", "{D178061B-84D3-44F9-BEED-EFD18D9033F0}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cpp-optparse", "..\Externals\cpp-optparse\cpp-optparse.vcxproj", "{C636D9D1-82FE-42B5-9987-63B7D4836341}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -230,6 +232,10 @@ Global
{D178061B-84D3-44F9-BEED-EFD18D9033F0}.Debug|x64.Build.0 = Debug|x64
{D178061B-84D3-44F9-BEED-EFD18D9033F0}.Release|x64.ActiveCfg = Release|x64
{D178061B-84D3-44F9-BEED-EFD18D9033F0}.Release|x64.Build.0 = Release|x64
{C636D9D1-82FE-42B5-9987-63B7D4836341}.Debug|x64.ActiveCfg = Debug|x64
{C636D9D1-82FE-42B5-9987-63B7D4836341}.Debug|x64.Build.0 = Debug|x64
{C636D9D1-82FE-42B5-9987-63B7D4836341}.Release|x64.ActiveCfg = Release|x64
{C636D9D1-82FE-42B5-9987-63B7D4836341}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -266,5 +272,6 @@ Global
{BB00605C-125F-4A21-B33B-7BF418322DCB} = {87ADDFF9-5768-4DA2-A33B-2477593D6677}
{570215B7-E32F-4438-95AE-C8D955F9FCA3} = {AAD1BCD6-9804-44A5-A5FC-4782EA00E9D4}
{D178061B-84D3-44F9-BEED-EFD18D9033F0} = {87ADDFF9-5768-4DA2-A33B-2477593D6677}
{C636D9D1-82FE-42B5-9987-63B7D4836341} = {87ADDFF9-5768-4DA2-A33B-2477593D6677}
EndGlobalSection
EndGlobal