Add command line parsing.

This commit is contained in:
Gerhard Hoffmann 2023-11-16 14:13:49 +01:00
parent 8bc86c6a94
commit bc864c7e8b
2 changed files with 265 additions and 0 deletions

208
commandline_parser.cpp Normal file
View File

@ -0,0 +1,208 @@
#include "commandline_parser.h"
#include <QDir>
#include <QSettings>
#include <QDebug>
#include <QFile>
CommandLineParser::CommandLineParser()
: m_repositoryUrl("")
, m_plugInDir("")
, m_plugInName("")
, m_workingDir("")
, m_dryRun("")
, m_noUpdatePsaHardware("")
, m_showYoctoVersion("")
, m_showYoctoInstallStatus("")
, m_showExtendedVersion("")
, m_iniFileName("")
, m_repositoryUrlOption(
QCommandLineOption(
QStringList() << "repository-url" << "repository-url",
QCoreApplication::translate("main", "Where to find a customer repository."),
QCoreApplication::translate("main", "directory")))
, m_iniFileDirectoryOption(
QCommandLineOption(
QStringList() << "ini-directory" << "ini-directory",
QCoreApplication::translate("main", "Where to find an ini-file."),
QCoreApplication::translate("main", "directory")))
, m_iniFileNameOption(
QCommandLineOption(
QStringList() << "ini-filename" << "ini-filename",
QCoreApplication::translate("main", "Name of ini-file."),
QCoreApplication::translate("main", "file")))
, m_pluginDirectoryOption(
QCommandLineOption(
QStringList() << "plugin-directory" << "plugin-directory",
QCoreApplication::translate("main", "Where to find dc-plugin."),
QCoreApplication::translate("main", "directory")))
, m_pluginNameOption(
QCommandLineOption(
QStringList() << "plugin-name" << "plugin-name",
QCoreApplication::translate("main", "Name of dc-plugin."),
QCoreApplication::translate("main", "directory")))
, m_noDownloadOption(
QCommandLineOption(
"no-psa-hardware-update",
QCoreApplication::translate("main", "Do not update the PSA firmware (json, device-controller).")))
, m_workingDirectoryOption(
QCommandLineOption(
QStringList() << "working-directory" << "working-directory",
QCoreApplication::translate("main", "working directory of update-script."),
QCoreApplication::translate("main", "directory")))
, m_dryRunOption(
QCommandLineOption(
QStringList() << "d" << "dry-run",
QCoreApplication::translate("main", "Start ATBUpdateTool in dry-run-mode. No actual actions.")))
, m_extendedVersionOption(
QCommandLineOption(
QStringList() << "V" << "extended-version",
QCoreApplication::translate("main", "Show extended version (including last git commit).")))
, m_yoctoVersionOption(
QCommandLineOption(
QStringList() << "y" << "yocto-version",
QCoreApplication::translate("main", "Show yocto version of ATBUpdateTool.")))
, m_yoctoInstallStatusOption(
QCommandLineOption(
QStringList() << "Y" << "yocto-install",
QCoreApplication::translate("main", "Show yocto install status of ATBUpdateTool."))) {
configure();
}
void CommandLineParser::configure() {
m_parser.setApplicationDescription("Download tool for downloading device controller firmware, printer json-files and executing opkg-commands.");
m_parser.addHelpOption();
m_parser.addVersionOption();
m_repositoryUrlOption.setDefaultValue("https://git.mimbach49.de/GerhardHoffmann");
m_parser.addOption(m_repositoryUrlOption);
m_iniFileDirectoryOption.setDefaultValue(".");
m_parser.addOption(m_iniFileDirectoryOption);
m_iniFileNameOption.setDefaultValue("ATBUpdateTool.ini");
m_parser.addOption(m_iniFileNameOption);
m_pluginDirectoryOption.setDefaultValue("/usr/lib");
m_parser.addOption(m_pluginDirectoryOption);
m_pluginNameOption.setDefaultValue("libCAslave.so");
m_parser.addOption(m_pluginNameOption);
m_parser.addOption(m_noDownloadOption);
m_workingDirectoryOption.setDefaultValue("/opt/app/tools/atbupdate");
m_parser.addOption(m_workingDirectoryOption);
m_parser.addOption(m_dryRunOption);
m_parser.addOption(m_extendedVersionOption);
m_parser.addOption(m_yoctoVersionOption);
m_parser.addOption(m_yoctoInstallStatusOption);
}
void CommandLineParser::readSettings() {
QString const iniFileDir = m_parser.value(m_iniFileDirectoryOption);
QString const iniFileName = m_parser.value(m_iniFileNameOption);
m_iniFileName = QDir::cleanPath(iniFileDir + QDir::separator() + iniFileName);
if (!m_iniFileName.isEmpty()) {
if (QFile(m_iniFileName).exists()) {
QSettings settings(m_iniFileName, QSettings::IniFormat);
QStringList keys = settings.allKeys();
for (QString const &key: keys) {
QVariant v = settings.value(key);
if (key.contains("repository-url")) {
m_repositoryUrl = v.toString();
} else
if (key.contains("plugin-directory")) {
m_plugInDir = v.toString();
} else
if (key.contains("working-directory")) {
m_plugInName = v.toString();
} else
if (key.contains("dry-run")) {
m_dryRun = (v.toBool() ? "true" : "false");
} else
if (key.contains("extended-version")) {
m_showExtendedVersion = (v.toBool() ? "true" : "false");
} else
if (key.contains("no-psa-hardware-update")) {
m_noUpdatePsaHardware = (v.toBool() ? "true" : "false");
} else
if (key.contains("yocto-install")) {
m_showYoctoInstallStatus = (v.toBool() ? "true" : "false");
} else
if (key.contains("yocto-version")) {
m_showYoctoVersion = (v.toBool() ? "true" : "false");
} else
if (key.contains("plugin-name")) {
m_plugInName = v.toString();
}
}
}
}
}
QString CommandLineParser::repositoryUrl() {
return m_repositoryUrl;
if (m_repositoryUrl.isEmpty()) {
m_repositoryUrl = m_parser.value(m_repositoryUrlOption);
}
return m_repositoryUrl;
}
QString CommandLineParser::plugInDir() {
if (m_plugInDir.isEmpty()) {
m_plugInDir = m_parser.value(m_pluginDirectoryOption);
}
return m_plugInDir;
}
QString CommandLineParser::plugInName() {
if (m_plugInName.isEmpty()) {
m_plugInName = m_parser.value(m_pluginNameOption);
}
return m_plugInName;
}
QString CommandLineParser::workingDir() {
if (m_workingDir.isEmpty()) {
m_workingDir = m_parser.value(m_workingDirectoryOption);
}
return m_workingDir;
}
bool CommandLineParser::dryRun() {
if (m_dryRun.isEmpty()) {
m_dryRun = m_parser.value(m_dryRunOption);
}
return m_dryRun == "false" ? false : true;
}
bool CommandLineParser::noUpdatePsaHardware() {
if (m_noUpdatePsaHardware.isEmpty()) {
m_noUpdatePsaHardware = m_parser.value(m_noDownloadOption);
}
return m_noUpdatePsaHardware == "false" ? false : true;
}
bool CommandLineParser::yoctoVersion() {
if (m_showYoctoVersion.isEmpty()) {
m_showYoctoVersion = m_parser.value(m_yoctoVersionOption);
}
return m_showYoctoVersion == "false" ? false : true;
}
bool CommandLineParser::yoctoInstallStatus() {
if (m_showYoctoInstallStatus.isEmpty()) {
m_showYoctoInstallStatus = m_parser.value(m_yoctoInstallStatusOption);
}
return m_showYoctoInstallStatus == "false" ? false : true;
}
bool CommandLineParser::extendedVersion() {
if (m_showExtendedVersion.isEmpty()) {
m_showExtendedVersion = m_parser.value(m_extendedVersionOption);
}
return m_showExtendedVersion == "false" ? false : true;
}

57
commandline_parser.h Normal file
View File

@ -0,0 +1,57 @@
#ifndef COMMAND_LINE_PARSER_H_INCLUDED
#define COMMAND_LINE_PARSER_H_INCLUDED
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QString>
class CommandLineParser : public QCommandLineParser {
QString m_repositoryUrl;
QString m_plugInDir;
QString m_plugInName;
QString m_workingDir;
QString m_dryRun;
QString m_noUpdatePsaHardware;
QString m_showYoctoVersion;
QString m_showYoctoInstallStatus;
QString m_showExtendedVersion;
QString m_iniFileName;
QCommandLineOption m_repositoryUrlOption;
QCommandLineOption m_iniFileDirectoryOption;
QCommandLineOption m_iniFileNameOption;
QCommandLineOption m_pluginDirectoryOption;
QCommandLineOption m_pluginNameOption;
QCommandLineOption m_noDownloadOption;
QCommandLineOption m_workingDirectoryOption;
QCommandLineOption m_dryRunOption;
QCommandLineOption m_extendedVersionOption;
QCommandLineOption m_yoctoVersionOption;
QCommandLineOption m_yoctoInstallStatusOption;
QCommandLineParser m_parser;
void configure();
public:
explicit CommandLineParser();
~CommandLineParser() = default;
QCommandLineParser &parser() { return m_parser; }
QCommandLineParser const &parser() const { return m_parser; }
void process(const QCoreApplication &app) { m_parser.process(app); }
QString const &iniFileName() const { return m_iniFileName; }
void readSettings();
QString repositoryUrl();
QString plugInDir();
QString plugInName();
QString workingDir();
bool dryRun();
bool noUpdatePsaHardware();
bool yoctoVersion();
bool yoctoInstallStatus();
bool extendedVersion();
};
#endif // COMMAND_LINE_PARSER_H_INCLUDED