refactor: Restructure parsing and setup loading methods

This commit is contained in:
Lars Toenning
2024-02-15 00:01:31 +01:00
parent 99de8009be
commit 9d3ae3e2b3
8 changed files with 50 additions and 34 deletions

View File

@@ -349,12 +349,7 @@ namespace BlackCore
{ {
m_started = false; // reset m_started = false; // reset
// parse if needed, parsing contains its own error handling Q_ASSERT_X(m_parsed, Q_FUNC_INFO, "Call this function after parsing");
if (!m_parsed)
{
const bool s = this->parseAndStartupCheck();
if (!s) { return false; }
}
// parsing itself is done // parsing itself is done
CStatusMessageList msgs; CStatusMessageList msgs;
@@ -1339,18 +1334,18 @@ namespace BlackCore
return m_parser.value(option).trimmed(); return m_parser.value(option).trimmed();
} }
bool CApplication::parseAndStartupCheck() bool CApplication::parseCommandLineArgsAndLoadSetup()
{
if (!this->startupCheck()) return false;
if (!this->parseCommandLineArguments()) return false;
if (!this->loadSetupAndHandleErrors()) return false;
return true;
}
bool CApplication::parseCommandLineArguments()
{ {
if (m_parsed) { return m_parsed; } // already done if (m_parsed) { return m_parsed; } // already done
// checks
const QStringList verifyErrors = CSwiftDirectories::verifyRuntimeDirectoriesAndFiles();
if (!verifyErrors.isEmpty() && !m_applicationInfo.isUnitTest())
{
this->cmdLineErrorMessage("Missing runtime directories/files:", verifyErrors.join(", "));
return false;
}
// we call parse because we also want to display a GUI error message when applicable // we call parse because we also want to display a GUI error message when applicable
const QStringList args(QCoreApplication::instance()->arguments()); const QStringList args(QCoreApplication::instance()->arguments());
if (!m_parser.parse(args)) if (!m_parser.parse(args))
@@ -1390,9 +1385,19 @@ namespace BlackCore
return true; return true;
} }
bool CApplication::parseAndLoadSetup() bool CApplication::startupCheck() const
{
const QStringList verifyErrors = CSwiftDirectories::verifyRuntimeDirectoriesAndFiles();
if (!verifyErrors.isEmpty() && !m_applicationInfo.isUnitTest())
{
cmdLineErrorMessage("Missing runtime directories/files:", verifyErrors.join(", "));
return false;
}
return true;
}
bool CApplication::loadSetupAndHandleErrors()
{ {
if (!this->parseAndStartupCheck()) return false;
const CStatusMessageList msgs = loadSetup(); const CStatusMessageList msgs = loadSetup();
if (msgs.isFailure()) if (msgs.isFailure())

View File

@@ -287,16 +287,13 @@ namespace BlackCore
//! Delegates to QCommandLineParser::value //! Delegates to QCommandLineParser::value
QString getParserValue(const QCommandLineOption &option) const; QString getParserValue(const QCommandLineOption &option) const;
//! Parses and handles the standard options such as help, version, parse error //! Combined function that does a startup check, parses the command line arguments and loads the setup
//! \note in some cases (error, version, help) application is terminated during this step //! \see startupCheck
//! \sa parsingHookIn //! \see parseCommandLineArguments
//! \return true means to continue, false to stop //! \see loadSetupAndHandleErrors
bool parseAndStartupCheck(); //! \remark This function cannot automatically called from the constructor because (1) it calls virtual
//! functions to show error messages and (2) the some command line arguments are added after constructing the object
//! Combined function bool parseCommandLineArgsAndLoadSetup();
//! \see parseAndStartupCheck
//! \see loadSetup
bool parseAndLoadSetup();
//! Display warning message //! Display warning message
virtual bool cmdLineWarningMessage(const QString &text, const QString &informativeText = "") const; virtual bool cmdLineWarningMessage(const QString &text, const QString &informativeText = "") const;
@@ -712,6 +709,20 @@ namespace BlackCore
//! Write meta information into the application directory so other swift versions can display them //! Write meta information into the application directory so other swift versions can display them
void tagApplicationDataDirectory(); void tagApplicationDataDirectory();
//! Check if all required runtime files are in place
//! \returns true if the check succeeded
bool startupCheck() const;
//! Loads the setup (bootstrap) and handles/displays warnings and error messages
//! \returns true if loading succedded without errors
bool loadSetupAndHandleErrors();
//! Parses and handles the standard options such as help, version, parse error
//! \note in some cases (error, version, help) application is terminated during this step
//! \sa parsingHookIn
//! \return true means to continue, false to stop
bool parseCommandLineArguments();
CInputManager *m_inputManager = nullptr; //!< Input devices and hotkeys CInputManager *m_inputManager = nullptr; //!< Input devices and hotkeys
QNetworkConfigurationManager *m_networkConfigManager = nullptr; //!< configuration QNetworkConfigurationManager *m_networkConfigManager = nullptr; //!< configuration
QNetworkAccessManager *m_accessManager = nullptr; //!< single network access manager QNetworkAccessManager *m_accessManager = nullptr; //!< single network access manager

View File

@@ -34,7 +34,7 @@ int main(int argc, char *argv[])
a.addDBusAddressOption(); a.addDBusAddressOption();
a.addVatlibOptions(); a.addVatlibOptions();
a.addAudioOptions(); a.addAudioOptions();
if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; } if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; }
const QString dBusAdress(a.getCmdDBusAddressValue()); const QString dBusAdress(a.getCmdDBusAddressValue());
a.useContexts(CCoreFacadeConfig::forCoreAllLocalInDBus(dBusAdress)); a.useContexts(CCoreFacadeConfig::forCoreAllLocalInDBus(dBusAdress));

View File

@@ -25,8 +25,8 @@ int main(int argc, char *argv[])
CCrashHandler::instance()->init(); CCrashHandler::instance()->init();
CGuiApplication a(CApplicationInfo::swiftMappingTool(), CApplicationInfo::MappingTool, CIcons::swiftDatabase48()); CGuiApplication a(CApplicationInfo::swiftMappingTool(), CApplicationInfo::MappingTool, CIcons::swiftDatabase48());
if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; }
a.splashScreen(CIcons::swiftDatabase256()); a.splashScreen(CIcons::swiftDatabase256());
if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; }
a.useWebDataServices(BlackCore::CWebReaderFlags::AllSwiftDbReaders, CDatabaseReaderConfigList::forMappingTool()); a.useWebDataServices(BlackCore::CWebReaderFlags::AllSwiftDbReaders, CDatabaseReaderConfigList::forMappingTool());
a.useFacadeNoContexts(); a.useFacadeNoContexts();
if (!a.start()) if (!a.start())

View File

@@ -37,10 +37,10 @@ int main(int argc, char *argv[])
int r = 0; int r = 0;
{ {
CSwiftGuiStdApplication a; // application with contexts CSwiftGuiStdApplication a; // application with contexts
if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; }
a.splashScreen(CIcons::swift256()); a.splashScreen(CIcons::swift256());
a.setMinimumSizeInCharacters(60, 42); // experimental a.setMinimumSizeInCharacters(60, 42); // experimental
if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; } if (!a.start())
if (!a.hasSetupReader() || !a.start())
{ {
a.gracefulShutdown(); a.gracefulShutdown();
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@@ -30,7 +30,7 @@ int main(int argc, char *argv[])
CGuiApplication a(CApplicationInfo::swiftLauncher(), CApplicationInfo::Laucher, CIcons::swiftLauncher1024()); CGuiApplication a(CApplicationInfo::swiftLauncher(), CApplicationInfo::Laucher, CIcons::swiftLauncher1024());
a.addVatlibOptions(); // so it can be passed (hand over) to started applications a.addVatlibOptions(); // so it can be passed (hand over) to started applications
a.addParserOption({ { "i", "installer" }, QCoreApplication::translate("main", "Installer setup.") }); a.addParserOption({ { "i", "installer" }, QCoreApplication::translate("main", "Installer setup.") });
if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; } if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; }
a.useWebDataServices(BlackCore::CWebReaderFlags::AllSwiftDbReaders, CDatabaseReaderConfigList::forLauncher()); a.useWebDataServices(BlackCore::CWebReaderFlags::AllSwiftDbReaders, CDatabaseReaderConfigList::forLauncher());
a.useFacadeNoContexts(); a.useFacadeNoContexts();
if (!a.start()) if (!a.start())

View File

@@ -100,7 +100,7 @@ int main(int argc, char *argv[])
BLACKTEST_INIT(BlackCoreTest::CTestContext) BLACKTEST_INIT(BlackCoreTest::CTestContext)
CApplication a(CApplicationInfo::UnitTest); CApplication a(CApplicationInfo::UnitTest);
a.addVatlibOptions(); a.addVatlibOptions();
const bool setup = a.parseAndLoadSetup(); const bool setup = a.parseCommandLineArgsAndLoadSetup();
if (!setup) { qWarning() << "No setup loaded"; } if (!setup) { qWarning() << "No setup loaded"; }
int r = EXIT_FAILURE; int r = EXIT_FAILURE;
if (a.start()) if (a.start())

View File

@@ -124,7 +124,7 @@ int main(int argc, char *argv[])
BLACKTEST_INIT(BlackCoreTest::CTestConnectivity) BLACKTEST_INIT(BlackCoreTest::CTestConnectivity)
CApplication a(CApplicationInfo::UnitTest); CApplication a(CApplicationInfo::UnitTest);
a.addVatlibOptions(); a.addVatlibOptions();
const bool setup = a.parseAndLoadSetup(); const bool setup = a.parseCommandLineArgsAndLoadSetup();
if (!setup) { qWarning() << "No setup loaded"; } if (!setup) { qWarning() << "No setup loaded"; }
int r = EXIT_FAILURE; int r = EXIT_FAILURE;
if (a.start()) if (a.start())