Skip to content

refactor: use stderr for CLI error output#3372

Open
sanjana2505006 wants to merge 1 commit intoapache:unstablefrom
sanjana2505006:fix/cli-error-output-improvement
Open

refactor: use stderr for CLI error output#3372
sanjana2505006 wants to merge 1 commit intoapache:unstablefrom
sanjana2505006:fix/cli-error-output-improvement

Conversation

@sanjana2505006
Copy link

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 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.

@sanjana2505006 sanjana2505006 changed the title Refactor CLI output: Use stderr for errors and improve usage message refactor: use stderr for CLI error output Feb 15, 2026
@sanjana2505006
Copy link
Author

@git-hulk Whenever you get a chance, please review and let me know if any changes are needed

<< 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can just change it to std::cerr?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually went with the std::ostream parameter because I wanted to stick to the standard way most CLI tools handle output. Usually, when someone asks for help explicitly (like with --help), they expect it on stdout so they can pipe it to grep or less. But if they just mess up a command, the usage warning should definitely go to stderr.

I also noticed that --version uses stdout right now, so this keeps things consistent.
Let me know if that makes sense! I'm happy to simplify it to just std::cerr if you'd prefer less boilerplate, but I figured this separation was worth the extra bit of code.

@sanjana2505006
Copy link
Author

@PragmaTwice whenever you get a chance, please let me know your thoughts on this. If you still think we should just use std::cerr for simplicity, I'm happy to make that change!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants