-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
configure output #1387
configure output #1387
Conversation
This comment has been minimized.
This comment has been minimized.
I think, we need to add commander.configureOutput({
writeErr: (code, message) => {
if ('commander.help' !== code) {
const prefix = '[' + (new Date()).toISOString() + '] ';
const applyStyle = str => '\x1b[31m' + str + '\x1b[0m';
message = applyStyle(prefix + message);
}
process.stderr.write(message);
},
}); |
I was not designing this form of However, the suggestion to add |
I decided adding Instead, I am trying an
(To give an idea of the pattern, there could be an |
So @cravler example could be: commander.configureOutput({
outputError: (message, write) => {
const prefix = '[' + (new Date()).toISOString() + '] ';
const applyStyle = str => '\x1b[31m' + str + '\x1b[0m';
write(applyStyle(prefix + message));
},
}); Does that look useful? |
Another naming possibility for "getOutColumns". |
Readme.md
Outdated
writeOut: (str) => process.stdout.write(`[OUT] ${str}`), | ||
writeErr: (str) => process.stdout.write(`[ERR] ${str}`), | ||
// Output errors in red. | ||
outputError: (str, write) => write(red(str)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found it difficult to understand the red
function because it came out of nowhere.
How about including the following in the readme code examples as well?
function red(str) {
return `\x1b[31m${str}\x1b[0m`;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good feedback, will do. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched name to errorColor
(which was what I called this in one of my own programs!) and added implementation to README as suggested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Thanks @abetomo |
Included in v7.0.0-1: https://github.com/tj/commander.js/releases/tag/v7.0.0-1 |
Pull Request
Problem
console.error
for errors andprocess.stderr.write
for help displayed as an errorconsole.error
for errors andprocess.stdout.write
for non-errorsstdout.columns
even when when displaying onstderr
Issues: #1241 #1370 #1371
Solution
Add
.configureOutput()
to control output for unit tests or custom implementations. By default useprocess.stderr.write
rather thanconsole.error
for displaying errors. IncludesoutputError
for easy customisation.I do not think the out/err customisation will be a widely used feature, but improves support for full custom implementations and unit testing with the pair of
.configureOutput()
and.exitOveride()
.The default implementations are:
Note:
getOutColumns
andgetErrColumns
later renamed togetOutHelpWidth
andgetErrHelpWidth
. See #1396To Do
Release Notes
.configureOutput()
to modify use ofstdout
andstderr
or customise display of errors (configure output #1387)