This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 3b02893fb refactor: use stderr for CLI error output (#3372)
3b02893fb is described below

commit 3b02893fbcd1e30b707f6d2f09fda01e63394551
Author: Sanjana <[email protected]>
AuthorDate: Thu Mar 12 20:23:33 2026 +0530

    refactor: use stderr for CLI error output (#3372)
    
    I noticed that the CLI was printing error messages (like invalid
    arguments or config load failures) to `stdout`, which isn't ideal for
    scripting or standard error handling. This PR redirects those critical
    errors to `stderr`.
    
    I also enhanced the
    
[PrintUsage](cci:1://file:///Users/sanjana./kvrocks/src/cli/main.cc:62:0-73:1)
    function to accept an output stream, so `-h`/`--help` still goes to
    `stdout` (as expected), but incorrect usage warnings go to `stderr`. The
    usage message itself has been slightly polished for better readability.
    
    Changes:
    - PrintUsage now takes an optional `std::ostream&` argument (defaults to
    `cout`).
    - Invalid command-line arguments now print usage to `cerr` and return
    exit code 1.
    - Config loading and logger initialization errors now print to `cerr`.
    - Added `<iostream>` include for correctness.
    
    ---------
    
    Co-authored-by: 纪华裕 <[email protected]>
---
 src/cli/main.cc | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/src/cli/main.cc b/src/cli/main.cc
index 53c951c55..40a3a960e 100644
--- a/src/cli/main.cc
+++ b/src/cli/main.cc
@@ -28,6 +28,7 @@
 #include <event2/thread.h>
 
 #include <iomanip>
+#include <iostream>
 #include <memory>
 #include <ostream>
 
@@ -59,15 +60,17 @@ struct NewOpt {
   friend auto &operator<<(std::ostream &os, NewOpt) { return os << 
std::string(4, ' ') << std::setw(32); }
 } new_opt;
 
-static void PrintUsage(const char *program) {
-  std::cout << program << " implements the Redis protocol based on rocksdb" << 
std::endl
-            << "Usage:" << std::endl
-            << std::left << new_opt << "-c, --config <filename>" << "set 
config file to <filename>, or `-` for stdin"
-            << std::endl
-            << new_opt << "-v, --version" << "print version information" << 
std::endl
-            << new_opt << "-h, --help" << "print this help message" << 
std::endl
-            << new_opt << "--<config-key> <config-value>"
-            << "overwrite specific config option <config-key> to 
<config-value>" << std::endl;
+static void PrintUsage(const char *program, std::ostream &os = std::cout) {
+  os << program << " implements the Redis protocol based on RocksDB" << 
std::endl
+     << "Usage:" << std::endl
+     << std::left << new_opt << "-c, --config <filename>"
+     << "set config file to <filename>, or `-` for stdin" << std::endl
+     << new_opt << "-v, --version"
+     << "print version information" << std::endl
+     << new_opt << "-h, --help"
+     << "print this help message" << std::endl
+     << new_opt << "--<config-key> <config-value>"
+     << "overwrite specific config option <config-key> to <config-value>" << 
std::endl;
 }
 
 static CLIOptions ParseCommandLineOptions(int argc, char **argv) {
@@ -87,7 +90,7 @@ static CLIOptions ParseCommandLineOptions(int argc, char 
**argv) {
       auto key = std::string_view(argv[i] + 2);
       opts.cli_options.emplace_back(key, argv[++i]);
     } else {
-      PrintUsage(*argv);
+      PrintUsage(*argv, std::cerr);
       std::exit(1);
     }
   }
@@ -152,7 +155,7 @@ int main(int argc, char *argv[]) {
   Config config;
   Status s = config.Load(opts);
   if (!s.IsOK()) {
-    std::cout << "Failed to load config. Error: " << s.Msg() << std::endl;
+    std::cerr << "Failed to load config. Error: " << s.Msg() << std::endl;
     return 1;
   }
   const auto socket_fd_exit = MakeScopeExit([&config] {
@@ -162,7 +165,7 @@ int main(int argc, char *argv[]) {
   });
 
   if (auto s = InitSpdlog(config); !s) {
-    std::cout << "Failed to initialize logging system. Error: " << s.Msg() << 
std::endl;
+    std::cerr << "Failed to initialize logging system. Error: " << s.Msg() << 
std::endl;
     return 1;
   }
   INFO("kvrocks {}", PrintVersion());

Reply via email to