added custom argument parser

This commit is contained in:
Syping 2020-10-16 06:29:55 +02:00
parent d96bc26b97
commit 866f0f1874

View file

@ -75,6 +75,62 @@ void catchUnixSignals(std::initializer_list<int> quitSignals) {
}
#endif
QStringList parseStringArguments(const QString &string)
{
QString argument;
bool slashMode = false;
bool dQuoteMode = false;
bool sQuoteMode = false;
QStringList argumentList;
for (const QChar &strChar : string) {
if (!slashMode && !dQuoteMode && !sQuoteMode) {
if (strChar == ' ') {
if (!argument.isEmpty()) {
argumentList << argument;
argument.clear();
}
}
else if (strChar == '\"') {
dQuoteMode = true;
}
else if (strChar == '\'') {
sQuoteMode = true;
}
else if (strChar == '\\') {
slashMode = true;
}
else {
argument += strChar;
}
}
else if (slashMode) {
argument += strChar;
slashMode = false;
}
else if (dQuoteMode) {
if (strChar == '\"') {
dQuoteMode = false;
}
else {
argument += strChar;
}
}
else if (sQuoteMode) {
if (strChar == '\'') {
sQuoteMode = false;
}
else {
argument += strChar;
}
}
}
if (slashMode || dQuoteMode || sQuoteMode)
return QStringList();
if (!argument.isEmpty())
argumentList << argument;
return argumentList;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
@ -112,7 +168,17 @@ int main(int argc, char *argv[])
argumentList = arguments;
}
else {
// Not yet
if (likely(!envExecutable.isEmpty())) {
executable = QString::fromUtf8(envExecutable);
}
if (likely(!envArguments.isEmpty())) {
argumentList = parseStringArguments(QString::fromUtf8(envArguments));
if (argumentList.empty()) {
QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl;
return 1;
}
}
}
if (unlikely(!envTimeout.isEmpty())) {
@ -339,8 +405,18 @@ int main(int argc, char *argv[])
}
}
else if (unlikely(commandLineParser.isSet(processArguments))) {
QTextStream(stderr) << "Arguments over command line are not supported yet!" << smsub_endl;
return 1;
argumentList = parseStringArguments(commandLineParser.value(processArguments));
if (argumentList.empty()) {
QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl;
return 1;
}
}
else if (unlikely(!envArguments.isEmpty())) {
argumentList = parseStringArguments(QString::fromUtf8(envArguments));
if (argumentList.empty()) {
QTextStream(stderr) << "Arguments can't be parsed properly!" << smsub_endl;
return 1;
}
}
if (likely(commandLineParser.isSet(subprocessSocket))) {