From ee7f7e7e232b251d3274975eb8aac57fbd17ae2e Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Thu, 14 Aug 2025 22:05:29 -0400 Subject: [PATCH] bees: handle --version and --help/-h before bees initialisation Introduce the standard --version option to print version and exit 0. Adjust --help/-h to do the same. Scan argv in main() for standard options before calling bees_main(). This avoids going through config parsing and Chatter log setup that we're going to throw away immediately. If neither option appears, write the version on stderr as before. Since the version string may be critical to debugging the configuration parsing code, the version string output is unconditional, and occurs prior to the start of config parsing. Resolves: https://github.com/Zygo/bees/issues/330 Signed-off-by: Zygo Blaxell --- src/bees-usage.txt | 3 ++- src/bees.cc | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/bees-usage.txt b/src/bees-usage.txt index b402bb03..705e4aa6 100644 --- a/src/bees-usage.txt +++ b/src/bees-usage.txt @@ -5,7 +5,8 @@ fs-root-path MUST be the root of a btrfs filesystem tree (subvol id 5). Other directories will be rejected. Options: - -h, --help Show this help + -h, --help Show this help and exit + --version Print version information and exit Load management options: -c, --thread-count Worker thread count (default CPU count * factor) diff --git a/src/bees.cc b/src/bees.cc index f66c607c..39f57ea0 100644 --- a/src/bees.cc +++ b/src/bees.cc @@ -34,10 +34,19 @@ using namespace crucible; using namespace std; +static +void +do_cmd_version() +{ + cout << "bees version " << BEES_VERSION << endl; +} + +static void do_cmd_help(char *argv[]) { - fprintf(stderr, BEES_USAGE, argv[0]); + do_cmd_version(); + fprintf(stdout, BEES_USAGE, argv[0]); } // static inline helpers ---------------------------------------- @@ -965,6 +974,20 @@ bees_main(int argc, char *argv[]) int main(int argc, char *argv[]) { + // Handle --version and --help before any bees-specific initialisation. + for (int i = 1; i < argc; ++i) { + const string argvi = argv[i]; + if (argvi == "--version") { + do_cmd_version(); + return EXIT_SUCCESS; + } + if (argvi == "--help" || argvi == "-h") { + do_cmd_help(argv); + return EXIT_SUCCESS; + } + } + + // Unconditional version banner on log output cerr << "bees version " << BEES_VERSION << endl; if (argc < 2) { -- 2.53.0