- Formatted all CodeGen module files (9 files) - Formatted LtsAppDescriptor module files - Formatted LtsAppManager/AppChooser files - Applied clang-format to all DTC and enum headers across modules - Removed coordTransTests from CommonUnitTestsPlaceholder (moved to tests/) Cosmetic changes only, no functional modifications. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
181 lines
5.6 KiB
C++
181 lines
5.6 KiB
C++
|
|
|
|
#include "LtsCommandLineParser.h"
|
|
|
|
#include <QFlags>
|
|
#include <QNetworkInterface>
|
|
#include <QSettings>
|
|
#include <QUrl>
|
|
#include <ngltools/ngltools_filesystem.h>
|
|
|
|
namespace LtsCommandLineParser
|
|
{
|
|
static constexpr int s_maxDomainId = 232;
|
|
|
|
LtsCommandLineParser::LtsCommandLineParser(ProvideCommandLineParameterOptions opts)
|
|
: QCommandLineParser(), m_enabledOptions(opts)
|
|
{
|
|
if (NGL::isAppInBuildBasePath() || m_enabledOptions.testFlag(CreateDomainIdFromIp))
|
|
{
|
|
const QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
|
|
for (const QHostAddress& address : addresses)
|
|
{
|
|
if (address.isLoopback() || address.isNull())
|
|
continue;
|
|
if (address.protocol() != QAbstractSocket::IPv4Protocol)
|
|
continue;
|
|
|
|
m_generatedDomainId = (address.toIPv4Address() & 0x000000ff) % 232;
|
|
if (m_generatedDomainId == 0)
|
|
m_generatedDomainId = s_maxDomainId;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (m_enabledOptions.testFlag(ExerciseId))
|
|
addOption({{QStringLiteral("e"), QStringLiteral("exerciseid")},
|
|
QStringLiteral("Default: -1 (invalid, get from lts-cc.conf)"),
|
|
QStringLiteral("exerciseId"),
|
|
QStringLiteral("-1")});
|
|
if (m_enabledOptions.testFlag(DomainId))
|
|
addOption({{QStringLiteral("d"), QStringLiteral("domainid")},
|
|
QStringLiteral("Default: ") + QString::number(m_generatedDomainId),
|
|
QStringLiteral("domainId"),
|
|
QString::number(m_generatedDomainId)});
|
|
if (m_enabledOptions.testFlag(CustomConfigFile))
|
|
{
|
|
const QString cf(NGL::findProjectConfigFile());
|
|
addOption({{QStringLiteral("i"), QStringLiteral("ini")},
|
|
QStringLiteral("Default: ") + cf,
|
|
QStringLiteral("filename"),
|
|
cf});
|
|
}
|
|
if (m_enabledOptions.testFlag(TcpConnectionUrl))
|
|
{
|
|
const QString defaultTcpUrl(QStringLiteral("EXCTDS01:7400"));
|
|
addOption({{QStringLiteral("t"), QStringLiteral("tcpurl")},
|
|
QStringLiteral("Default: ") + defaultTcpUrl,
|
|
QStringLiteral("hostname:port"),
|
|
defaultTcpUrl});
|
|
}
|
|
addHelpOption();
|
|
|
|
if (!m_enabledOptions.testFlag(NoVersion))
|
|
addVersionOption();
|
|
}
|
|
|
|
int LtsCommandLineParser::domainId() const
|
|
{
|
|
if (m_domainId == 0 && m_generatedDomainId != 0 && !m_domainIdSetByFunction)
|
|
{
|
|
#ifdef WIN32
|
|
|
|
fprintf(stdout,
|
|
"\n\nAttention: using a default domain id != 0 because executable is run inside builddir - default "
|
|
"domainid is %d\n\n",
|
|
m_generatedDomainId);
|
|
#else
|
|
fprintf(stdout,
|
|
"\n\n\e[31mAttention: using a default domain id != 0 because executable is run inside builddir - "
|
|
"default domainid is %d\e[0m\n\n",
|
|
m_generatedDomainId);
|
|
#endif
|
|
return m_generatedDomainId;
|
|
}
|
|
return m_domainId;
|
|
}
|
|
|
|
void LtsCommandLineParser::setDomainId(int id)
|
|
{
|
|
m_domainIdSetByFunction = true;
|
|
m_domainId = id;
|
|
}
|
|
|
|
bool LtsCommandLineParser::iniFileSet() const
|
|
{
|
|
if (!m_enabledOptions.testFlag(CustomConfigFile))
|
|
return false;
|
|
return isSet(QStringLiteral("i"));
|
|
}
|
|
|
|
bool LtsCommandLineParser::useTcpConnection() const
|
|
{
|
|
if (!m_enabledOptions.testFlag(TcpConnectionUrl))
|
|
return false;
|
|
return isSet(QStringLiteral("t"));
|
|
}
|
|
|
|
QUrl LtsCommandLineParser::tcpConnectionUrl() const
|
|
{
|
|
if (!m_enabledOptions.testFlag(TcpConnectionUrl))
|
|
return QUrl();
|
|
if (!useTcpConnection())
|
|
return QUrl();
|
|
QString str = value(QStringLiteral("t"));
|
|
|
|
str.replace(QLatin1String("tcpv4_lan"), QLatin1String("tcpv4lan"));
|
|
QUrl url = QUrl::fromUserInput(str);
|
|
if (url.scheme() == QLatin1String("http") || url.scheme() == QLatin1String("file"))
|
|
url.setScheme(QStringLiteral("tcpv4lan"));
|
|
return url;
|
|
}
|
|
|
|
bool LtsCommandLineParser::init(bool bParseHelp)
|
|
{
|
|
if (!parse(QCoreApplication::arguments()))
|
|
{
|
|
fprintf(stdout, "\nBad option: %s \n", qPrintable(errorText()));
|
|
if (!m_unitModuleTestMode)
|
|
return false;
|
|
}
|
|
|
|
if (m_enabledOptions.testFlag(ExerciseId))
|
|
m_exerciseId = value(QStringLiteral("e")).toInt();
|
|
if (m_enabledOptions.testFlag(DomainId))
|
|
m_domainId = value(QStringLiteral("d")).toInt();
|
|
if (m_enabledOptions.testFlag(CustomConfigFile))
|
|
m_iniFile = value(QStringLiteral("i"));
|
|
|
|
if (bParseHelp && isSet(QStringLiteral("h")))
|
|
showHelp();
|
|
if (!m_unitModuleTestMode && isSet(QStringLiteral("v")))
|
|
showVersion();
|
|
if (!domainIdProvided())
|
|
{
|
|
const QString iniFile = m_iniFile.isEmpty() ? NGL::findProjectConfigFile() : m_iniFile;
|
|
if (!iniFile.isEmpty())
|
|
{
|
|
QSettings s(iniFile, QSettings::IniFormat);
|
|
bool bOk;
|
|
int domainId = s.value(QStringLiteral("Global/DomainId"), m_domainId).toInt(&bOk);
|
|
if (bOk && domainId > 0 && domainId <= s_maxDomainId)
|
|
m_domainId = domainId;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void LtsCommandLineParser::showHelp(int exitCode)
|
|
{
|
|
if (!m_additionalHelpStr.isEmpty())
|
|
fprintf(stdout, "%s", qPrintable(m_additionalHelpStr));
|
|
|
|
QCommandLineParser::showHelp(exitCode);
|
|
}
|
|
|
|
bool LtsCommandLineParser::domainIdProvided() const
|
|
{
|
|
if (!m_enabledOptions.testFlag(DomainId))
|
|
return false;
|
|
return isSet(QStringLiteral("d"));
|
|
}
|
|
|
|
bool LtsCommandLineParser::exerciseIdProvided() const
|
|
{
|
|
if (!m_enabledOptions.testFlag(ExerciseId))
|
|
return false;
|
|
return isSet(QStringLiteral("e"));
|
|
}
|
|
|
|
} // namespace LtsCommandLineParser
|