I'm writing command line utilities in Node.js. Here's a snippet that is
a source of some concern for me.
try {
options = require('arguable')(__filename);
} catch (error) {
console.error('error: ' + e.message);
console.error(e.usage);
process.exit(1);
}
frobinate(options.severity);
Essentially, I'm calling an options parser and finding errors that
prevent me form continuing. I want to exit with a non-zero exit code.
However, if I exit abruptly, I find that the console is not always
flushed. Sometimes on Windows. Sometimes when piping stderr.
I'm wondering when I can count on a flush of stderr before exit, when I
can't, and what you feel is the right way to exit from a command line
program, if you have any thoughts on the matter.
Currently, I'm creating a main function, surrounding that in a try/catch
block, and throwing special exceptions to indicate that the error does
not involve a stack trace.
--
Alan Gutierrez -
http://github.com/bigeasy -
http://twitter.com/bigeasy