node-argp
node-argp copied to clipboard
small change needed for headless use
After studying the code, it appears nearly flexible enough to be used in a headless environment. Even though it contains calls to console, the library will not talk to the console if you supply a handler for the error event, and the handler is supplied the error string for its own purposes.
Unfortunately, there are three exceptions. help and usage cases call console.log and provide no way for the library user to prevent that and receive the help/usage string. (For reasons I cannot discern, printVersion chooses to call process.stdout.write instead of console.log.) Conceptually, one might view help/usage/version as not an "error" and therefore not expect it to go to the error handler. But probably, most headless applications (if indeed there are any besides me!) will want to just treat them the same as they do errors: display the text and know that you're not getting a parsed command (the library internally pretends they are errors in order to cease scanning arguments).
I could nearly get the behavior I need by just tweaking all three locations to call this._error with their information string. Unfortunately, that inserts the "Error: " string in front of the desired output string. The least hack I could see that seemed most consistent with the current design was to add a new event:
Argp.prototype._info = function(str){
if(this.listeners("info").length)
return this.emit("info", str);
else
console.log(str);
}
I then replaced the three offending output calls with:
this._info(str_formerly_going_to_console)