From 1ace03d4b52ff13db35c2053651f9ae1eaccd3d9 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Thu, 3 Apr 2025 18:34:45 -0400 Subject: [PATCH] feat: add help and version to gui, move no-reset to the QCommandLineProcessor --- src/apps/CMakeLists.txt | 2 +- src/apps/deskflow-gui/CMakeLists.txt | 16 +------- src/apps/deskflow-gui/deskflow-gui.cpp | 40 ++++++++++++++----- .../deskflow-gui/deskflow.man.template.in | 9 ----- 4 files changed, 31 insertions(+), 36 deletions(-) delete mode 100755 src/apps/deskflow-gui/deskflow.man.template.in diff --git a/src/apps/CMakeLists.txt b/src/apps/CMakeLists.txt index 03d5eb2c2..0467d26a1 100644 --- a/src/apps/CMakeLists.txt +++ b/src/apps/CMakeLists.txt @@ -14,7 +14,7 @@ function(generate_app_man TARGET) if(HELP2MAN) add_custom_command( TARGET ${target} POST_BUILD - COMMAND ${HELP2MAN} + COMMAND QT_QPA_PLATFORM=minimal ${HELP2MAN} --include ${CMAKE_SOURCE_DIR}/src/apps/res/manpage.txt --no-info $ diff --git a/src/apps/deskflow-gui/CMakeLists.txt b/src/apps/deskflow-gui/CMakeLists.txt index 01419d0f8..f5344b980 100644 --- a/src/apps/deskflow-gui/CMakeLists.txt +++ b/src/apps/deskflow-gui/CMakeLists.txt @@ -84,19 +84,5 @@ elseif(APPLE) install(TARGETS ${target} BUNDLE DESTINATION .) else() install(TARGETS ${target} DESTINATION bin) - if(HELP2MAN) - configure_file(deskflow.man.template.in ${CMAKE_CURRENT_BINARY_DIR}/${target}) - add_custom_command( - TARGET ${target} POST_BUILD - COMMAND ${HELP2MAN} - --version-string "${CMAKE_PROJECT_VERSION}" - --include ${CMAKE_SOURCE_DIR}/src/apps/res/manpage.txt - --no-info ${CMAKE_CURRENT_BINARY_DIR}/${target} - -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.1 - ) - install( - FILES ${CMAKE_CURRENT_BINARY_DIR}/${target}.1 - DESTINATION ${CMAKE_INSTALL_MANDIR}/man1 - ) - endif() + generate_app_man(${target}) endif() diff --git a/src/apps/deskflow-gui/deskflow-gui.cpp b/src/apps/deskflow-gui/deskflow-gui.cpp index 3a90b18ed..bfc6fa15f 100644 --- a/src/apps/deskflow-gui/deskflow-gui.cpp +++ b/src/apps/deskflow-gui/deskflow-gui.cpp @@ -16,6 +16,7 @@ #include "gui/StyleUtils.h" #include +#include #include #include #include @@ -36,11 +37,6 @@ using namespace deskflow::gui; bool checkMacAssistiveDevices(); #endif -bool hasArg(const QString &arg, const QStringList &args) -{ - return std::ranges::any_of(args, [&arg](const QString &a) { return a == arg; }); -} - int main(int argc, char *argv[]) { #if defined(Q_OS_UNIX) && defined(QT_DEBUG) @@ -50,13 +46,37 @@ int main(int argc, char *argv[]) QCoreApplication::setApplicationName(kAppName); QCoreApplication::setOrganizationName(kAppName); + QCoreApplication::setApplicationVersion(kVersion); + QCoreApplication::setOrganizationDomain(kOrgDomain); // used in prefix, can't be a url QGuiApplication::setDesktopFileName(QStringLiteral("org.deskflow.deskflow")); - // used as a prefix for settings paths, and must not be a url. - QCoreApplication::setOrganizationDomain(kOrgDomain); - QApplication app(argc, argv); + // Add Command Line Options + QCommandLineOption helpOption = QCommandLineOption("help", "Display Help on the command line"); + QCommandLineOption versionOption = QCommandLineOption("version", "Display version information"); + QCommandLineOption noResetOption = + QCommandLineOption("no-reset", "Prevent settings reset if DESKFLOW_RESET_ALL is set"); + + QCommandLineParser parser; + parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); + parser.addOption(helpOption); + parser.addOption(versionOption); + parser.addOption(noResetOption); + parser.parse(QCoreApplication::arguments()); + + const auto header = QStringLiteral("%1: %2\n").arg(kAppName, kDisplayVersion); + if (parser.isSet(helpOption) || !parser.unknownOptionNames().isEmpty() || !parser.errorText().isEmpty()) { + QTextStream(stdout) << header << QStringLiteral(" %1\n\n").arg(kAppDescription) + << parser.helpText().replace(QApplication::applicationFilePath(), kAppId); + return 0; + } + + if (parser.isSet(versionOption)) { + QTextStream(stdout) << header << kCopyright << Qt::endl; + return 0; + } + // Create a shared memory segment with a unique key // This is to prevent a new instance from running if one is already running QSharedMemory sharedMemory("deskflow-gui"); @@ -113,10 +133,8 @@ int main(int argc, char *argv[]) #endif // --no-reset - QStringList arguments = QCoreApplication::arguments(); - const auto noReset = hasArg("--no-reset", arguments); const auto resetEnvVar = QVariant(qEnvironmentVariable("DESKFLOW_RESET_ALL")).toBool(); - if (resetEnvVar && !noReset) { + if (resetEnvVar && !parser.isSet(noResetOption)) { diagnostic::clearSettings(false); } diff --git a/src/apps/deskflow-gui/deskflow.man.template.in b/src/apps/deskflow-gui/deskflow.man.template.in deleted file mode 100755 index 9e00b6f3a..000000000 --- a/src/apps/deskflow-gui/deskflow.man.template.in +++ /dev/null @@ -1,9 +0,0 @@ -# SPDX-FileCopyrightText: Deskflow Developers -# SPDX-License-Identifier: MIT -#!/bin/sh -eu - -if [ "$1" = '--help' ]; then -cat << EOM -@CMAKE_PROJECT_DESCRIPTION@. This is the GUI. It takes no arguments or options. -EOM -fi