Reimplement ArgumentParser in C++

Makes it possible to use --help and --version even if another instance
of Moment is already running. Fixes #122.
This commit is contained in:
DrRac27
2022-02-09 00:08:44 +01:00
parent 66afe32c8b
commit f24e5a707b
4 changed files with 43 additions and 64 deletions

View File

@@ -1,59 +0,0 @@
// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
// and Moment contributors <https://gitlab.com/mx-moment/moment>
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma Singleton
import QtQuick 2.12
QtObject {
property bool startInTray: false
property string loadQml: ""
readonly property string help: `Usage: ${Qt.application.name} [options]
Options:
-t, --start-in-tray Start in the system tray, without a visible window
-l, --load-qml PATH Override the file to be loaded as src/gui/UI.qml
-V, --version Show the application's version and exit
-h, --help Show this help and exit
Environment variables:
MOMENT_CONFIG_DIR Override the default configuration folder
MOMENT_DATA_DIR Override the default application data folder
MOMENT_CACHE_DIR Override the default cache and downloads folder
http_proxy Override the General.proxy setting, see settings.py
`
readonly property bool ready: {
const passedArguments = Qt.application.arguments.slice(1)
while (passedArguments.length) {
switch (passedArguments.shift()) {
case "-h":
case "--help":
print("\n\n" + help.replace(/^ {4}/gm, ""))
Qt.quit()
break
case "-v":
case "--version":
print(Qt.application.version)
Qt.quit()
break
case "-t":
case "--start-in-tray":
startInTray = true
break
case "-l":
case "--load-qml":
loadQml = passedArguments.shift()
break
}
}
return true
}
}

View File

@@ -28,6 +28,8 @@ ApplicationWindow {
property var theme: null
property var themeRules: null
property string settingsFolder
property bool startInTray
property string loadQml
readonly property var visibleMenus: ({})
readonly property var visiblePopups: ({})
@@ -98,7 +100,7 @@ ApplicationWindow {
minimumHeight: theme ? theme.minimumSupportedHeight : 120
width: Math.min(screen.width, 1152)
height: Math.min(screen.height, 768)
visible: ArgumentParser.ready && ! ArgumentParser.startInTray
visible: ! startInTray
color: "transparent"
onClosing: if (py.ready && settings.General.close_to_tray) {
@@ -123,8 +125,8 @@ ApplicationWindow {
focus: true
scale: py.ready ? 1 : 0.5
source:
ArgumentParser.ready && py.ready ?
(ArgumentParser.loadQml || "UI.qml") :
py.ready ?
(loadQml || "UI.qml") :
""
Behavior on scale { HNumberAnimation { overshoot: 3; factor: 1.2 } }

View File

@@ -1,2 +1 @@
singleton ModelStore 0.1 ModelStore.qml
singleton ArgumentParser 0.1 ArgumentParser.qml