diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fbea669..83a635c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,18 @@ target_link_libraries(fstl Qt5::Widgets Qt5::Core Qt5::Gui Qt5::OpenGL ${OPENGL_ # Add version definitions to use within the code. target_compile_definitions(fstl PRIVATE -DFSTL_VERSION="${PROJECT_VERSION}") +# Enable a simple test that validates the CLI `--version` output is exact. +# Escape dots in PROJECT_VERSION so version is matched literally in the regex. Use character class to avoid backslash escaping issues. +string(REPLACE "." "[.]" PROJECT_VERSION_ESCAPED "${PROJECT_VERSION}") + +enable_testing() +add_test(NAME version_check COMMAND $ --version) +set_tests_properties(version_check PROPERTIES PASS_REGULAR_EXPRESSION "fstl ${PROJECT_VERSION_ESCAPED}") + +# Also validate the POSIX-compliant short alias `-V` produces the same output. +add_test(NAME version_check_V COMMAND $ -V) +set_tests_properties(version_check_V PROPERTIES PASS_REGULAR_EXPRESSION "fstl ${PROJECT_VERSION_ESCAPED}") + #installer information that is platform independent set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Fast .stl file viewer.") set(CPACK_PACKAGE_VERSION_MAJOR ${FSTL_VERSION_MAJOR}) diff --git a/src/main.cpp b/src/main.cpp index 3999846d..fd857ae9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,6 @@ +#include #include +#include #include "app.h" @@ -11,7 +13,27 @@ int main(int argc, char* argv[]) QCoreApplication::setOrganizationDomain("https://github.com/fstl-app/fstl"); QCoreApplication::setApplicationName("fstl"); QCoreApplication::setApplicationVersion(FSTL_VERSION); + App a(argc, argv); + // Set up QCommandLineParser to handle --help, --version, and file argument + QCommandLineParser parser; + parser.setApplicationDescription("Fast .stl file viewer"); + parser.addHelpOption(); + + // Add custom version option with both -V (uppercase, POSIX) and --version + QCommandLineOption versionOption(QStringList{"V", "version"}, "Displays version information."); + parser.addOption(versionOption); + + parser.addPositionalArgument("file", "STL file to open (optional)", "[file]"); + parser.process(a); + + // Handle version option manually since we use custom -V + if (parser.isSet(versionOption)) { + std::cout << QCoreApplication::applicationName().toStdString() << " " + << QCoreApplication::applicationVersion().toStdString() << "\n"; + return 0; + } + return a.exec(); }