diff --git a/src/commandline.cpp b/src/commandline.cpp index 1d44e0435..aaa31c8af 100644 --- a/src/commandline.cpp +++ b/src/commandline.cpp @@ -10,6 +10,8 @@ #include #include +#include + namespace cl { @@ -221,8 +223,12 @@ std::optional CommandLine::runEarly() std::optional CommandLine::runPostApplication(MOApplication& a) { - // handle -i with no arguments - if (m_vm.count("instance") && m_vm["instance"].as() == "") { + const auto instanceArg = m_vm.find("instance"); + if (instanceArg != m_vm.end() && + !instanceArg->second.as>().has_value()) { + + // handle -i with no arguments (distinct from -i "", which will launch the + // portable instance if it exists, hence the use of boost::optional) env::Console c; if (auto i = InstanceManager::singleton().currentInstance()) { @@ -314,7 +320,9 @@ void CommandLine::createOptions() ("logs", "duplicates the logs to stdout") - ("instance,i", po::value()->implicit_value(""), + ("instance,i", + po::value>()->implicit_value( + boost::none), "use the given instance (defaults to last used)") ("profile,p", po::value(), @@ -402,8 +410,14 @@ std::optional CommandLine::instance() const if (m_shortcut.isValid() && m_shortcut.hasInstance()) { return m_shortcut.instanceName(); - } else if (m_vm.count("instance")) { - return QString::fromStdString(m_vm["instance"].as()); + } else { + const auto instanceArg = m_vm.find("instance"); + if (instanceArg != m_vm.end()) { + const auto& instanceVal = instanceArg->second.as>(); + if (instanceVal.has_value()) { + return QString::fromStdString(instanceVal.value()); + } + } } return {};