ProgramExit::ProgramExit(int status, const std::string& msg)
: std::runtime_error(msg),
status_(status) {
- CHECK_NE(status, 0);
+ // Message is only allowed for non-zero exit status
+ CHECK(status_ != 0 || msg.empty());
}
NestedCommandLineApp::NestedCommandLineApp(
/**
* Exception that commands may throw to force the program to exit cleanly
- * with a non-zero exit code. NestedCommandLineApp::run() catches this and
+ * with a given exit code. NestedCommandLineApp::run() catches this and
* makes run() print the given message on stderr (followed by a newline, unless
- * empty), and return the exit code. (Other exceptions will propagate out of
- * run())
+ * empty; the message is only allowed when exiting with a non-zero status), and
+ * return the exit code. (Other exceptions will propagate out of run())
*/
class ProgramExit : public std::runtime_error {
public:
* Run the command and return; the return code is 0 on success or
* non-zero on error, so it is idiomatic to call this at the end of main():
* return app.run(argc, argv);
+ *
+ * On successful exit, run() will check for errors on stdout (and flush
+ * it) to help command-line applications that need to write to stdout
+ * (failing to write to stdout is an error). If there is an error on stdout,
+ * we'll print a helpful message on stderr and return an error status (1).
*/
int run(int argc, const char* const argv[]);
int run(const std::vector<std::string>& args);