Commit 9258b46e authored by Jan Möbius's avatar Jan Möbius
Browse files

Removed simpleopt and use Qt commandline parser instead

parent 36fd3158
......@@ -835,7 +835,7 @@ private slots:
* the current path will be prepended.
* @param _asPolyMesh Load as a polymesh ( yes/no)
*/
void commandLineOpen(const char* _filename, bool _asPolyMesh );
void commandLineOpen(const QString& _filename, bool _asPolyMesh );
/** \brief Load a script from the commandline on application start
*
......@@ -847,7 +847,7 @@ private slots:
* @param _filename filename of the script to be opened. If it does not contain a full path,
* the current path will be prepended.
*/
void commandLineScript(const char* _filename );
void commandLineScript(const QString& _filename );
private slots:
......@@ -861,10 +861,10 @@ private slots:
private:
/// Vector storing filenames from commandline to be opened after application startup (objects)
std::vector< std::pair < std::string , bool > > commandLineFileNames_;
std::vector< std::pair < QString , bool > > commandLineFileNames_;
/// Vector storing filenames from commandline to be opened after application startup (script files)
std::vector< std::string > commandLineScriptNames_;
std::vector< QString > commandLineScriptNames_;
public:
......
......@@ -99,9 +99,9 @@ void Core::slotGetAllFilters ( QStringList& _list){
}
}
void Core::commandLineOpen(const char* _filename, bool _asPolyMesh ){
void Core::commandLineOpen(const QString& _filename, bool _asPolyMesh ){
QString file(_filename);
QString file = _filename;
// Modify filename to contain full paths if they were given as relative paths
if ( !file.startsWith("/") && !file.contains(":") ) {
......@@ -111,12 +111,12 @@ void Core::commandLineOpen(const char* _filename, bool _asPolyMesh ){
}
// Add to the open list
commandLineFileNames_.push_back(std::pair< std::string , bool >(file.toStdString(), _asPolyMesh));
commandLineFileNames_.push_back(std::pair< QString , bool >(file, _asPolyMesh));
}
void Core::commandLineScript(const char* _filename ) {
void Core::commandLineScript(const QString& _filename ) {
QString file(_filename);
QString file = _filename;
// Modify filename to contain full paths if they were given as relative paths
if ( !file.startsWith("/") && !file.contains(":") ) {
......@@ -126,7 +126,7 @@ void Core::commandLineScript(const char* _filename ) {
}
// Add to the open list
commandLineScriptNames_.push_back(file.toStdString());
commandLineScriptNames_.push_back(file);
}
void Core::slotExecuteAfterStartup() {
......@@ -168,7 +168,7 @@ void Core::slotExecuteAfterStartup() {
for ( uint i = 0 ; i < commandLineFileNames_.size() ; ++i ) {
// Skip scripts here as they will be handled by a different function
QString tmp = QString::fromStdString(commandLineFileNames_[i].first);
QString tmp = commandLineFileNames_[i].first;
if ( tmp.endsWith("ofs",Qt::CaseInsensitive) ) {
commandLineScriptNames_.push_back(commandLineFileNames_[i].first);
continue;
......@@ -176,9 +176,9 @@ void Core::slotExecuteAfterStartup() {
// If the file was given with the polymesh option, open them as polymeshes.
if (commandLineFileNames_[i].second)
loadObject(typeId("PolyMesh"), QString::fromStdString(commandLineFileNames_[i].first));
loadObject(typeId("PolyMesh"), commandLineFileNames_[i].first);
else {
loadObject(QString::fromStdString(commandLineFileNames_[i].first));
loadObject(commandLineFileNames_[i].first);
}
}
......@@ -190,7 +190,7 @@ void Core::slotExecuteAfterStartup() {
// If we have scripting support, execute the scripts given at the commandline.
if ( scriptingSupport )
for ( uint i = 0 ; i < commandLineScriptNames_.size() ; ++i ) {
emit executeFileScript(QString::fromStdString(commandLineScriptNames_[i]));
emit executeFileScript(commandLineScriptNames_[i]);
}
// If we don't have a gui and we are not under remote control,
......
......@@ -54,7 +54,6 @@ set (directories
../Scripting
../Scripting/scriptPrototypes
../Scripting/scriptWrappers
../SimpleOpt
../widgets/aboutWidget
../widgets/addEmptyWidget
../widgets/loggerWidget
......
......@@ -60,7 +60,8 @@
// stdc++
#include <csignal>
#include <regex>
#include <OpenFlipper/SimpleOpt/SimpleOpt.h>
#include <QCommandLineParser>
#if ( defined(WIN32))
#define NO_EXECINFO
......@@ -287,145 +288,133 @@ void segfaultHandling (int) {
std::abort();
}
enum {OPT_HELP , OPT_STEREO, OPT_BATCH ,OPT_CONSOLE_LOG , OPT_DEBUGGING, OPT_FULLSCREEN,
OPT_HIDDDEN_LOGGER , OPT_NOSPLASH ,OPT_HIDDDEN_TOOLBOX , OPT_LOAD_POLYMESHES,
OPT_REMOTE, OPT_REMOTE_PORT};
CSimpleOpt::SOption g_rgOptions[] = {
{ OPT_DEBUGGING , (char*) "--debug" , SO_NONE },
{ OPT_HELP , (char*) "-?" , SO_NONE },
{ OPT_HELP , (char*) "--help" , SO_NONE },
{ OPT_HELP , (char*) "-h" , SO_NONE },
{ OPT_STEREO , (char*) "--disable-stereo" , SO_NONE },
{ OPT_BATCH , (char*) "-b" , SO_NONE },
{ OPT_CONSOLE_LOG , (char*) "-c" , SO_NONE },
{ OPT_CONSOLE_LOG , (char*) "--log-to-console" , SO_NONE },
{ OPT_FULLSCREEN , (char*) "-f" , SO_NONE },
{ OPT_HIDDDEN_LOGGER , (char*) "-l" , SO_NONE },
{ OPT_NOSPLASH , (char*) "--no-splash" , SO_NONE },
{ OPT_HIDDDEN_TOOLBOX , (char*) "-t" , SO_NONE },
{ OPT_LOAD_POLYMESHES , (char*) "-p" , SO_NONE },
{ OPT_REMOTE , (char*) "--remote-control" , SO_NONE },
{ OPT_REMOTE_PORT , (char*) "--remote-port" , SO_REQ_SEP },
SO_END_OF_OPTIONS // END
enum CommandLineParseResult
{
CommandLineOk,
CommandLineError,
CommandLineVersionRequested,
CommandLineHelpRequested
};
void showHelp() {
std::cerr << "OpenFlipper [Options] <filenames> " << std::endl << std::endl;;
std::cerr << "Possible Options : " << std::endl;
std::cerr << std::endl;
std::cerr << "Load/Save Options:" << std::endl;
std::cerr << " -p \t: Open files as PolyMeshes" << std::endl;
std::cerr << std::endl;
std::cerr << "Gui Options:" << std::endl;
std::cerr << " -f \t\t: Start Fullscreen" << std::endl;
std::cerr << " -l \t\t: Start with hidden logger" << std::endl;
std::cerr << " -t \t\t: Start with hidden Toolbox" << std::endl;
std::cerr << " --no-splash \t: Disable splash screen" << std::endl;
std::cerr << " --disable-stereo \t: Disable Stereo Mode" << std::endl;
std::cerr << std::endl;
std::cerr << "Log options:" << std::endl;
std::cerr << " --log-to-console ( -c ) \t: Write logger window contents to console" << std::endl;
std::cerr << std::endl;
bool openPolyMeshes = false;
bool remoteControl = false;
std::cerr << "Other options:" << std::endl;
std::cerr << " -b \t: Batch mode, you have to provide a script for execution" << std::endl;
std::cerr << " --remote-control \t: Batch mode accepting remote connections" << std::endl;
// Parse all options
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, QString *errorMessage) {
std::cerr << std::endl;
#ifndef WIN32
#ifndef __APPLE__
//workaround for bug with stereo mode on Qt5.7.0 and Qt5.7.1 on Linux
int QtVersionMajor, QtVersionMinor, QtVersionPatch;
if(sscanf(qVersion(),"%1d.%1d.%1d",&QtVersionMajor, &QtVersionMinor, &QtVersionPatch) == 3)
{
if(QtVersionMajor == 5 && QtVersionMinor >= 7)
{
if(QtVersionPatch < 2)
{
std::cerr << "The used Qt Version does not support stereo mode. Disabling stereo mode." << std::endl;
OpenFlipper::Options::stereo(false);
}
else
std::cerr << "Stereo Mode has not been tested for the used Qt Version." << std::endl;
}
}
#endif
#endif
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.addOptions({
{{"d", "debug"},
QCoreApplication::translate("main", "Enable debugging mode")},
{"disable-stereo",
QCoreApplication::translate("main", "Disable stereo mode")},
{{"b", "batch"},
QCoreApplication::translate("main", "Batch mode, you have to provide a script for execution")},
{{"c", "log-to-console"},
QCoreApplication::translate("main", "Write logger window contents to console")},
{"remote-control",
QCoreApplication::translate("main", "Batch mode accepting remote connections")},
{{"f", "fullscreen"},
QCoreApplication::translate("main", "Start in fulscreen mode")},
{{"l", "hide-logger"},
QCoreApplication::translate("main", "Start with hidden log window")},
{{"t", "hide-toolbox"},
QCoreApplication::translate("main", "Start with hidden toolbox")},
{"no-splash",
QCoreApplication::translate("main", "Hide splash screen")},
{"p",
QCoreApplication::translate("main", "Open files as PolyMeshes")},
{"remote-port",
QCoreApplication::translate("main", "Remote port"),"portnumber"},
});
const QCommandLineOption helpOption = parser.addHelpOption();
const QCommandLineOption versionOption = parser.addVersionOption();
// Now parse the command line
if (!parser.parse(QCoreApplication::arguments())) {
*errorMessage = parser.errorText();
return CommandLineError;
}
if (parser.isSet(helpOption))
return CommandLineHelpRequested;
std::cerr << " -h \t: This help" << std::endl;
}
if (parser.isSet(versionOption))
return CommandLineVersionRequested;
if (parser.isSet("debug")) {
OpenFlipper::Options::debug(true);
}
if (parser.isSet("disable-stereo")) {
OpenFlipper::Options::stereo(false);
}
if (parser.isSet("batch")) {
OpenFlipper::Options::nogui(true);
}
if (parser.isSet("log-to-console")) {
OpenFlipper::Options::logToConsole(true);
}
if (parser.isSet("remote-control")) {
OpenFlipper::Options::remoteControl(true);
}
bool openPolyMeshes = false;
bool remoteControl = false;
if (parser.isSet("fullscreen")) {
OpenFlipperSettings().setValue("Core/Gui/fullscreen",true);
}
bool parseCommandLineOptions(CSimpleOpt& args){
if (parser.isSet("hide-logger")) {
OpenFlipper::Options::loggerState(OpenFlipper::Options::Hidden);
}
QString port;
if (parser.isSet("hide-toolbox")) {
OpenFlipperSettings().setValue("Core/Gui/ToolBoxes/hidden",true);
}
#ifndef WIN32
#ifndef __APPLE__
//workaround for bug with stereo mode on Qt5.7.0 and Qt5.7.1 on Linux
int QtVersionMajor, QtVersionMinor, QtVersionPatch;
if(sscanf(qVersion(),"%1d.%1d.%1d",&QtVersionMajor, &QtVersionMinor, &QtVersionPatch) == 3)
{
if(QtVersionMajor == 5 && QtVersionMinor >= 7)
{
if(QtVersionPatch < 2)
{
std::cerr << "The used Qt Version does not support stereo mode. Disabling stereo mode." << std::endl;
OpenFlipper::Options::stereo(false);
}
else
std::cerr << "Stereo Mode has not been tested for the used Qt Version." << std::endl;
}
if (parser.isSet("no-splash")) {
OpenFlipperSettings().setValue("Core/Gui/splash",false);
}
#endif
#endif
// while there are arguments left to process
while (args.Next()) {
if (parser.isSet("p")) {
openPolyMeshes = true;
}
if (args.LastError() == SO_SUCCESS) {
if (parser.isSet("remote-port")) {
const QString port = parser.value("portnumber");
std::cerr << "Got port option : " << port.toStdString() << std::endl;
OpenFlipper::Options::remoteControl(port.toInt());
switch (args.OptionId() ) {
case OPT_BATCH:
OpenFlipper::Options::nogui(true);
break;
case OPT_CONSOLE_LOG:
OpenFlipper::Options::logToConsole(true);
break;
case OPT_DEBUGGING:
OpenFlipper::Options::debug(true);
break;
case OPT_STEREO:
OpenFlipper::Options::stereo(false);
break;
case OPT_HIDDDEN_TOOLBOX:
OpenFlipperSettings().setValue("Core/Gui/ToolBoxes/hidden",true);
break;
case OPT_HIDDDEN_LOGGER:
OpenFlipper::Options::loggerState(OpenFlipper::Options::Hidden);
break;
case OPT_FULLSCREEN:
OpenFlipperSettings().setValue("Core/Gui/fullscreen",false);
break;
case OPT_LOAD_POLYMESHES:
openPolyMeshes = true;
break;
case OPT_NOSPLASH:
OpenFlipperSettings().setValue("Core/Gui/splash",false);
break;
case OPT_REMOTE:
OpenFlipper::Options::remoteControl(true);
break;
case OPT_REMOTE_PORT:
port = args.OptionArg();
std::cerr << "Got option : " << port.toStdString() << std::endl;
OpenFlipper::Options::remoteControl(port.toInt());
break;
case OPT_HELP:
showHelp();
return 0;
}
} else {
std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
showHelp();
return false;
}
}
return true;
return CommandLineOk;
}
int main(int argc, char **argv)
......@@ -448,16 +437,23 @@ int main(int argc, char **argv)
OpenFlipper::Options::argc(&argc);
OpenFlipper::Options::argv(&argv);
CSimpleOpt argBatch(argc, argv, g_rgOptions);
// Ugly command line parse to check if we run in batch mode or not.
// Qt parser needs either QApplication or QCoreApplication to work.
// But we need that option to decide which one to choose so ...
for (int i = 1; i < argc; i++) {
QString option = QString(argv[i]);
if (option.contains("batch") || option.contains("-b") ||
option.contains("--batch") || option.contains("/b") ||
option.contains("/batch" )) {
std::cerr << "Batch Mode started" << std::endl;
OpenFlipper::Options::nogui(true);
}
//check only batchMode before the core is created
while (argBatch.Next())
if (argBatch.OptionId() == OPT_BATCH ){
OpenFlipper::Options::nogui(true);
break;
}
}
QCommandLineParser parser;
QString errorMessage;
CSimpleOpt args(argc, argv, g_rgOptions);
#ifndef NO_CATCH_SIGSEGV
// Set a handler for segfaults
......@@ -490,9 +486,23 @@ int main(int argc, char **argv)
// create core ( this also reads the ini files )
Core * w = new Core( );
if ( !parseCommandLineOptions(args) ) {
delete w;
return 1;
switch (parseCommandLine(parser, &errorMessage)) {
case CommandLineOk:
break;
case CommandLineError:
fputs(qPrintable(errorMessage), stderr);
fputs("\n\n", stderr);
fputs(qPrintable(parser.helpText()), stderr);
delete w;
return 1;
case CommandLineVersionRequested:
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
qPrintable(QCoreApplication::applicationVersion()));
return 0;
case CommandLineHelpRequested:
parser.showHelp();
Q_UNREACHABLE();
}
#ifdef WIN32
......@@ -549,8 +559,11 @@ int main(int argc, char **argv)
initGlew();
#endif
for ( int i = 0 ; i < args.FileCount(); ++i )
w->commandLineOpen(args.File(i), openPolyMeshes);
const QStringList positionalArguments = parser.positionalArguments();
for ( auto file: positionalArguments ) {
w->commandLineOpen(file, openPolyMeshes);
}
return app.exec();
......@@ -567,16 +580,32 @@ int main(int argc, char **argv)
// create widget ( this also reads the ini files )
Core * w = new Core( );
if ( !parseCommandLineOptions(args) ) {
delete w;
return 1;
switch (parseCommandLine(parser, &errorMessage)) {
case CommandLineOk:
break;
case CommandLineError:
fputs(qPrintable(errorMessage), stderr);
fputs("\n\n", stderr);
fputs(qPrintable(parser.helpText()), stderr);
delete w;
return 1;
case CommandLineVersionRequested:
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
qPrintable(QCoreApplication::applicationVersion()));
return 0;
case CommandLineHelpRequested:
parser.showHelp();
Q_UNREACHABLE();
}
// After setting all Options from command line, build the real gui
w->init();
for ( int i = 0 ; i < args.FileCount(); ++i )
w->commandLineScript(args.File(i));
const QStringList positionalArguments = parser.positionalArguments();
for ( auto file: positionalArguments ) {
w->commandLineOpen(file, openPolyMeshes);
}
return app.exec();
}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -56,37 +56,6 @@ void CoreWidget::addCoreLicenseInfo() {
// Core License Information
// ======================================================================
// Simple opt used for parsing command line arguments across architectures
addAboutInfo("OpenFlipper uses SimpleOpt for platform independent command line parsing<br>"
"SimpleOpt is licensed under the MIT license ( see below)<br>"
"<br>"
"<b>Author:</b> Brodie Thiesfield [code at jellycan dot com]<br>"
"<b>Source:</b> http://code.jellycan.com/simpleopt/ <br>"
"<br>"
"The licence text below is the boilerplate \"<b>MIT Licence</b>\" used from:<br>"
"http://www.opensource.org/licenses/mit-license.php <br>"
"<br>"
"Copyright (c) 2006-2007, Brodie Thiesfield"
"<br>"
"Permission is hereby granted, free of charge, to any person obtaining a copy "
"of this software and associated documentation files (the \"Software\"), to "
"deal in the Software without restriction, including without limitation the "
"rights to use, copy, modify, merge, publish, distribute, sublicense, and/or "
"sell copies of the Software, and to permit persons to whom the Software is "
"furnished to do so, subject to the following conditions:<br><br>"
"The above copyright notice and this permission notice shall be included in "
"all copies or substantial portions of the Software."
"<br><br>"
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR "
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, "
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL "
"THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER "
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING "
"FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS "
"IN THE SOFTWARE."
,"SimpleOpt");
addAboutInfo("OpenFlipper uses Stackwalker for windows stack trace creation<br>"
"StackWalker is licensed under the BSD license ( see below)<br>"
"<br>"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment