Skip to content

Commit

Permalink
add commandlineparser.h
Browse files Browse the repository at this point in the history
  • Loading branch information
shogimaru committed Nov 4, 2023
1 parent feb5329 commit e820f8b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/commandlineparser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <QCommandLineOption>
#include <QCoreApplication>

#ifdef Q_OS_WASM

#include <QMap>

class CommandLineParser {
public:
void setApplicationDescription(const QString &) {}
void addHelpOption() {}
void addVersionOption() {}
bool addOption(const QCommandLineOption &) { return true; }
void process(const QCoreApplication &);
QString value(const QCommandLineOption &option) const;

private:
QMap<QString, QString> _values;
};


#else

#include <QCommandLineParser>

class CommandLineParser : public QCommandLineParser {
};

#endif
1 change: 1 addition & 0 deletions src/commandlineparser_native.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "commandlineparser.h"
30 changes: 30 additions & 0 deletions src/commandlineparser_wasm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "commandlineparser.h"
#include <emscripten/val.h>
#include <emscripten.h>


void CommandLineParser::process(const QCoreApplication &)
{
emscripten::val location = emscripten::val::global("location");
QString href = QString::fromStdString(location["href"].as<std::string>());
int idx = href.indexOf("?");

if (idx > 0) {
QStringList params = href.mid(idx + 1).split("&");
for (auto &param : params) {
idx = param.indexOf("=");
if (idx >= 0) {
_values.insert(param.mid(0, idx), param.mid(idx + 1));
qWarning() << param.mid(0, idx) << param.mid(idx + 1);
} else {
_values.insert(param, QString());
}
}
}
}


QString CommandLineParser::value(const QCommandLineOption &option) const
{
return _values.value(option.names().value(0));
}

0 comments on commit e820f8b

Please sign in to comment.