From 33475e2c367d3392966238f41b758aa2ed89e57b Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Fri, 5 Dec 2025 23:04:49 -0500 Subject: [PATCH] Factor out version-printing to usage.cpp (#1870) --- include/usage.hpp | 2 ++ src/asm/main.cpp | 5 ++--- src/fix/main.cpp | 3 +-- src/gfx/main.cpp | 5 ++--- src/link/main.cpp | 5 ++--- src/usage.cpp | 5 +++++ 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/include/usage.hpp b/include/usage.hpp index d9e45725..93c71664 100644 --- a/include/usage.hpp +++ b/include/usage.hpp @@ -13,6 +13,8 @@ struct Usage { std::vector flags; std::vector, std::vector>> options; + void printVersion(bool error) const; + [[noreturn]] void printAndExit(int code) const; diff --git a/src/asm/main.cpp b/src/asm/main.cpp index b908d5bb..c09ec58e 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -28,7 +28,6 @@ #include "usage.hpp" #include "util.hpp" // UpperMap #include "verbosity.hpp" -#include "version.hpp" #include "asm/charmap.hpp" #include "asm/fstack.hpp" @@ -296,7 +295,7 @@ static void parseArg(int ch, char *arg) { // LCOV_EXCL_START case 'V': - printf("%s %s\n", usage.name.c_str(), get_package_version_string()); + usage.printVersion(false); exit(0); case 'v': @@ -381,7 +380,7 @@ static void verboseOutputConfig() { style_Set(stderr, STYLE_MAGENTA, false); - fprintf(stderr, "%s %s\n", usage.name.c_str(), get_package_version_string()); + usage.printVersion(true); printVVVVVVerbosity(); diff --git a/src/fix/main.cpp b/src/fix/main.cpp index 5394bb12..3fbf1cf6 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -19,7 +19,6 @@ #include "style.hpp" #include "usage.hpp" #include "util.hpp" -#include "version.hpp" #include "fix/fix.hpp" #include "fix/mbc.hpp" @@ -252,7 +251,7 @@ static void parseArg(int ch, char *arg) { // LCOV_EXCL_START case 'V': - printf("%s %s\n", usage.name.c_str(), get_package_version_string()); + usage.printVersion(false); exit(0); case 'v': diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index 1d583f8b..48b1ce75 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -23,7 +23,6 @@ #include "usage.hpp" #include "util.hpp" #include "verbosity.hpp" -#include "version.hpp" #include "gfx/pal_spec.hpp" #include "gfx/process.hpp" @@ -409,7 +408,7 @@ static void parseArg(int ch, char *arg) { // LCOV_EXCL_START case 'V': - printf("%s %s\n", usage.name.c_str(), get_package_version_string()); + usage.printVersion(false); exit(0); case 'v': @@ -481,7 +480,7 @@ static void verboseOutputConfig() { style_Set(stderr, STYLE_MAGENTA, false); - fprintf(stderr, "%s %s\n", usage.name.c_str(), get_package_version_string()); + usage.printVersion(true); printVVVVVVerbosity(); diff --git a/src/link/main.cpp b/src/link/main.cpp index 6a084a83..6ca2c022 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -21,7 +21,6 @@ #include "usage.hpp" #include "util.hpp" // UpperMap, printChar #include "verbosity.hpp" -#include "version.hpp" #include "link/assign.hpp" #include "link/lexer.hpp" @@ -283,7 +282,7 @@ static void parseArg(int ch, char *arg) { // LCOV_EXCL_START case 'V': - printf("%s %s\n", usage.name.c_str(), get_package_version_string()); + usage.printVersion(false); exit(0); case 'v': @@ -330,7 +329,7 @@ static void verboseOutputConfig() { style_Set(stderr, STYLE_MAGENTA, false); - fprintf(stderr, "%s %s\n", usage.name.c_str(), get_package_version_string()); + usage.printVersion(true); printVVVVVVerbosity(); diff --git a/src/usage.cpp b/src/usage.cpp index 666a4178..5de44fa6 100644 --- a/src/usage.cpp +++ b/src/usage.cpp @@ -9,6 +9,7 @@ #include "helpers.hpp" #include "platform.hpp" #include "style.hpp" +#include "version.hpp" #if defined(_MSC_VER) || defined(__MINGW32__) #define WIN32_LEAN_AND_MEAN // Include less from `windows.h` @@ -17,6 +18,10 @@ #include #endif +void Usage::printVersion(bool error) const { + fprintf(error ? stderr : stdout, "%s %s\n", name.c_str(), get_package_version_string()); +} + void Usage::printAndExit(int code) const { FILE *file; bool isTerminal;