Use own command line parser.

This commit is contained in:
Gerhard Hoffmann 2023-11-16 14:16:31 +01:00
parent 792ff33482
commit 8fff5026df

119
main.cpp
View File

@ -10,6 +10,7 @@
#include "message_handler.h" #include "message_handler.h"
#include "plugins/interfaces.h" #include "plugins/interfaces.h"
#include "commandline_parser.h"
#include <unistd.h> #include <unistd.h>
#include <memory> #include <memory>
@ -21,6 +22,7 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QStandardPaths> #include <QStandardPaths>
#include <QMainWindow> #include <QMainWindow>
#include <QSettings>
#include "update.h" #include "update.h"
#include "git/git_client.h" #include "git/git_client.h"
@ -51,7 +53,7 @@ int main(int argc, char *argv[]) {
if (value != "C") { if (value != "C") {
qputenv("LC_ALL", "C"); qputenv("LC_ALL", "C");
} }
// qputenv("XDG_RUNTIME_DIR", "/run/user/0"); // qputenv("XDG_RUNTIME_DIR", "/var/run/user/0");
openlog("ATB-UPDATE", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER); openlog("ATB-UPDATE", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER);
@ -64,64 +66,44 @@ int main(int argc, char *argv[]) {
setDebugLevel(LOG_NOTICE); setDebugLevel(LOG_NOTICE);
} }
QCommandLineParser parser; CommandLineParser parser;
parser.setApplicationDescription("Download tool for downloading device controller firmware, printer json-files and executing opkg-commands.");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption pluginDirectoryOption(QStringList() << "plugin-directory" << "plugin-directory",
QCoreApplication::translate("main", "Where to find dc-plugin."),
QCoreApplication::translate("main", "directory"));
QString const pluginDefault = "/usr/lib";
pluginDirectoryOption.setDefaultValue(pluginDefault);
parser.addOption(pluginDirectoryOption);
QCommandLineOption pluginNameOption(QStringList() << "plugin-name" << "plugin-name",
QCoreApplication::translate("main", "Name of dc-plugin."),
QCoreApplication::translate("main", "directory"));
QString const pluginNameDefault = "libCAslave.so";
pluginNameOption.setDefaultValue(pluginNameDefault);
parser.addOption(pluginNameOption);
QCommandLineOption noDownloadOption("no-psa-hardware-update",
QCoreApplication::translate("main", "Do not update the PSA firmware (json, device-controller)."));
parser.addOption(noDownloadOption);
QCommandLineOption workingDirectoryOption(QStringList() << "working-directory" << "working-directory",
QCoreApplication::translate("main", "working directory of update-script."),
QCoreApplication::translate("main", "directory"));
QString const workingDirectoryDefault = "/opt/app/tools/atbupdate";
workingDirectoryOption.setDefaultValue(workingDirectoryDefault);
parser.addOption(workingDirectoryOption);
QCommandLineOption dryRunOption(QStringList() << "d" << "dry-run",
QCoreApplication::translate("main", "Start ATBUpdateTool in dry-run-mode. No actual actions."));
parser.addOption(dryRunOption);
QCommandLineOption extendedVersionOption(QStringList() << "V" << "extended-version",
QCoreApplication::translate("main", "Show extended version (including last git commit)."));
parser.addOption(extendedVersionOption);
QCommandLineOption yoctoVersionOption(QStringList() << "y" << "yocto-version",
QCoreApplication::translate("main", "Show yocto version of ATBUpdateTool."));
parser.addOption(yoctoVersionOption);
QCommandLineOption yoctoInstallStatusOption(QStringList() << "Y" << "yocto-install",
QCoreApplication::translate("main", "Show yocto install status of ATBUpdateTool."));
parser.addOption(yoctoInstallStatusOption);
// Process the actual command line arguments given by the user
parser.process(a); parser.process(a);
QString plugInDir = parser.value(pluginDirectoryOption); parser.readSettings();
QString plugInName = parser.value(pluginNameOption);
QString workingDir = parser.value(workingDirectoryOption); QString repositoryUrl = parser.repositoryUrl();
bool const dryRun = parser.isSet(dryRunOption); QString plugInDir = parser.plugInDir();
bool const noUpdatePsaHardware = parser.isSet(noDownloadOption); QString plugInName = parser.plugInName();
bool const showYoctoVersion = parser.isSet(yoctoVersionOption); QString workingDir = parser.workingDir();
bool const showYoctoInstallStatus = parser.isSet(yoctoInstallStatusOption); bool const dryRun = parser.dryRun();
bool const showExtendedVersion = parser.isSet(extendedVersionOption); bool const noUpdatePsaHardware = parser.noUpdatePsaHardware();
bool const showYoctoVersion = parser.yoctoVersion();
bool const showYoctoInstallStatus = parser.yoctoInstallStatus();
bool const showExtendedVersion = parser.extendedVersion();
QString const rtPath = QCoreApplication::applicationDirPath(); QString const rtPath = QCoreApplication::applicationDirPath();
qInfo() << "pwd ......................" << rtPath;
qInfo() << "repositoryUrl ............" << repositoryUrl;
qInfo() << "plugInDir ................" << plugInDir;
qInfo() << "plugInName ..............." << plugInName;
qInfo() << "workingDir ..............." << workingDir;
qInfo() << "dryRun ..................." << dryRun;
qInfo() << "noUpdatePsaHardware ......" << noUpdatePsaHardware;
qInfo() << "extended-version ........." << APP_EXTENDED_VERSION;
if (showExtendedVersion) {
printf(APP_EXTENDED_VERSION"\n");
return 0;
}
if (showYoctoVersion) {
printf("%s\n", Worker::getATBUpdateToolYoctoVersion().toStdString().c_str());
return 0;
}
if (showYoctoInstallStatus) {
printf("%s\n", Worker::getATBUpdateToolYoctoInstallationStatus().toStdString().c_str());
return 0;
}
int machineNr = Utils::read1stLineOfFile("/etc/machine_nr"); int machineNr = Utils::read1stLineOfFile("/etc/machine_nr");
int customerNr = Utils::read1stLineOfFile("/etc/cust_nr"); int customerNr = Utils::read1stLineOfFile("/etc/cust_nr");
int zoneNr = Utils::read1stLineOfFile("/etc/zone_nr"); int zoneNr = Utils::read1stLineOfFile("/etc/zone_nr");
@ -131,20 +113,6 @@ int main(int argc, char *argv[]) {
QThread::currentThread()->setObjectName("main thread"); QThread::currentThread()->setObjectName("main thread");
qInfo() << "Main thread" << QThread::currentThreadId(); qInfo() << "Main thread" << QThread::currentThreadId();
if (showExtendedVersion) {
printf(APP_EXTENDED_VERSION"\n");
return 0;
}
if (showYoctoVersion) {
printf("%s\n", Worker::getATBUpdateToolYoctoVersion().toStdString().c_str());
return 0;
}
if (showYoctoInstallStatus) {
printf("%s\n", Worker::getATBUpdateToolYoctoInstallationStatus().toStdString().c_str());
return 0;
}
if (!QDir(plugInDir).exists()) { if (!QDir(plugInDir).exists()) {
qCritical() << plugInDir qCritical() << plugInDir
@ -152,16 +120,6 @@ int main(int argc, char *argv[]) {
exit(-1); exit(-1);
} }
qInfo() << "pwd ......................" << rtPath;
qInfo() << "plugInDir ................" << plugInDir;
qInfo() << "plugInName ..............." << plugInName;
qInfo() << "workingDir ..............." << workingDir;
qInfo() << "dryRun ..................." << dryRun;
qInfo() << "noUpdatePsaHardware ......" << noUpdatePsaHardware;
qInfo() << "extended-version ........." << APP_EXTENDED_VERSION;
//qInfo() << "yocto-version ............" << Worker::getATBUpdateToolYoctoVersion();
//qInfo() << "yocto-install-status ....." << Worker::getATBUpdateToolYoctoInstallationStatus();
// before loading the library, delete all possible shared memory segments // before loading the library, delete all possible shared memory segments
#if defined Q_OS_LINUX || defined Q_OS_UNIX #if defined Q_OS_LINUX || defined Q_OS_UNIX
// system("rm -rf /tmp/qipc*"); // system("rm -rf /tmp/qipc*");
@ -172,6 +130,7 @@ int main(int argc, char *argv[]) {
Worker worker(customerNr, Worker worker(customerNr,
machineNr, machineNr,
zoneNr, zoneNr,
repositoryUrl,
branchName, branchName,
plugInDir, plugInDir,
plugInName, plugInName,