summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisanae <14251494+isanae@users.noreply.github.com>2020-11-07 17:21:13 -0500
committerisanae <14251494+isanae@users.noreply.github.com>2020-11-07 20:16:27 -0500
commitba7d24faef49858b9721c2ade0f3ea4a7ba4d96d (patch)
tree3eca105cdc03aee82eed15777f36504d32e106ea
parent1bf24c7daad1eb954dc160784802de58f9809fa1 (diff)
moved handleCommandLine() to CommandLine::setupCore()
-rw-r--r--src/commandline.cpp53
-rw-r--r--src/commandline.h22
-rw-r--r--src/main.cpp51
3 files changed, 76 insertions, 50 deletions
diff --git a/src/commandline.cpp b/src/commandline.cpp
index dc8cf53f..3d0e901d 100644
--- a/src/commandline.cpp
+++ b/src/commandline.cpp
@@ -1,10 +1,15 @@
#include "commandline.h"
#include "env.h"
+#include "organizercore.h"
#include "shared/util.h"
+#include <log.h>
+#include <report.h>
namespace cl
{
+using namespace MOBase;
+
std::string pad_right(std::string s, std::size_t n, char c=' ')
{
if (s.size() < n)
@@ -195,6 +200,54 @@ std::optional<int> CommandLine::run(const std::wstring& line)
}
}
+std::optional<int> CommandLine::setupCore(OrganizerCore& organizer) const
+{
+ // if we have a command line parameter, it is either a nxm link or
+ // a binary to start
+ if (m_shortcut.isValid()) {
+ if (m_shortcut.hasExecutable()) {
+ try {
+ organizer.processRunner()
+ .setFromShortcut(m_shortcut)
+ .setWaitForCompletion()
+ .run();
+
+ return 0;
+ }
+ catch (const std::exception &e) {
+ reportError(
+ QObject::tr("failed to start shortcut: %1").arg(e.what()));
+ return 1;
+ }
+ }
+ } else if (m_nxmLink) {
+ log::debug("starting download from command line: {}", *m_nxmLink);
+ organizer.externalMessage(*m_nxmLink);
+ } else if (m_executable) {
+ const QString exeName = *m_executable;
+ log::debug("starting {} from command line", exeName);
+
+ try
+ {
+ // pass the remaining parameters to the binary
+ organizer.processRunner()
+ .setFromFileOrExecutable(exeName, m_untouched)
+ .setWaitForCompletion()
+ .run();
+
+ return 0;
+ }
+ catch (const std::exception &e)
+ {
+ reportError(
+ QObject::tr("failed to start application: %1").arg(e.what()));
+ return 1;
+ }
+ }
+
+ return {};
+}
+
void CommandLine::clear()
{
m_vm.clear();
diff --git a/src/commandline.h b/src/commandline.h
index 478ee8ea..baa0bfb9 100644
--- a/src/commandline.h
+++ b/src/commandline.h
@@ -4,6 +4,8 @@
#include <vector>
#include <memory>
+class OrganizerCore;
+
namespace cl
{
@@ -193,6 +195,18 @@ protected:
// if moshortcut:// is detected and has an instance, it will override -i if both
// are given
//
+//
+// the command is used in two phases:
+//
+// 1) run() is called in main() shortly after startup; it parses the command
+// line and runs the given command, if any
+//
+// 2) if the command did not request to exit, the instance is loaded
+// in main() et al. and setupCore() is called; it handles moshortcut,
+// nxm links and starting executables/binaries
+//
+// either can return an exit code, which will make MO exit immediately
+//
class CommandLine
{
public:
@@ -206,6 +220,14 @@ public:
//
std::optional<int> run(const std::wstring& line);
+ // handles moshortcut, nxm links and starting processes
+ //
+ // returns an empty optional if execution should continue, or a return code
+ // if MO must quit
+ //
+ std::optional<int> setupCore(OrganizerCore& organizer) const;
+
+
// clears parsed options, used when MO is "restarted" so the options aren't
// processed again
//
diff --git a/src/main.cpp b/src/main.cpp
index e0326b27..55fedc7a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -115,55 +115,6 @@ void setExceptionHandlers()
g_prevTerminateHandler = std::set_terminate(onTerminate);
}
-std::optional<int> handleCommandLine(
- const cl::CommandLine& cl, OrganizerCore& organizer)
-{
- // if we have a command line parameter, it is either a nxm link or
- // a binary to start
- if (cl.shortcut().isValid()) {
- if (cl.shortcut().hasExecutable()) {
- try {
- organizer.processRunner()
- .setFromShortcut(cl.shortcut())
- .setWaitForCompletion()
- .run();
-
- return 0;
- }
- catch (const std::exception &e) {
- reportError(
- QObject::tr("failed to start shortcut: %1").arg(e.what()));
- return 1;
- }
- }
- } else if (cl.nxmLink()) {
- log::debug("starting download from command line: {}", *cl.nxmLink());
- organizer.externalMessage(*cl.nxmLink());
- } else if (cl.executable()) {
- const QString exeName = *cl.executable();
- log::debug("starting {} from command line", exeName);
-
- try
- {
- // pass the remaining parameters to the binary
- organizer.processRunner()
- .setFromFileOrExecutable(exeName, cl.untouched())
- .setWaitForCompletion()
- .run();
-
- return 0;
- }
- catch (const std::exception &e)
- {
- reportError(
- QObject::tr("failed to start application: %1").arg(e.what()));
- return 1;
- }
- }
-
- return {};
-}
-
int runApplication(
MOApplication &application, const cl::CommandLine& cl,
SingleInstance &instance, const QString &dataPath,
@@ -274,7 +225,7 @@ int runApplication(
organizer.setCurrentProfile(currentInstance.profileName());
- if (auto r=handleCommandLine(cl, organizer)) {
+ if (auto r=cl.setupCore(organizer)) {
return *r;
}