-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
So I'm trying to process the output from running commands, in this particular case for automated testing. I checked out this section of the README: https://github.com/tj/commander.js?tab=readme-ov-file#override-exit-and-output-handling and it says that you can use configureOutput to catch output from the commands going through stdout and stderr. I copied the example code and it does indeed work for the version and help commands, however for any command which prints to the console (which from the examples in the documentation appears to be the correct way to have your CLI app output information), the output bypasses configureOutput.
Here's a minimal repro app that closely mimics what my actual CLI does. Note that it takes advantage of how console.log will automatically format objects instead of just showing "[object Object]".
import * as commander from "commander";
const program = new commander.Command();
program.configureOutput({
// Visibly override write routines as example!
writeOut: (str: any) => process.stdout.write(`[OUT] ${str}`),
writeErr: (str: any) => process.stdout.write(`[ERR] ${str}`),
});
const logObject = {
uwot: "m8",
uavin: "a giggle"
}
program.version('1.2.3').option('-c, --compress').command('sub-command');
program.command("uwot").action((options, command) => {
console.log(logObject)
})
program.parse();
When I run my app I get the following output:
bun run configureOutputExample.ts uwot
{
uwot: "m8",
uavin: "a giggle",
}
which is the desired output, however I would expect the configured writeOut function to capture it and it doesn't.
I would like either for the configureOutput option to be able to capture console.log calls from the action function, or for Commander to include a logging function which will act like console.log by default but will be caught by configureOutput if set. In my particular case I would actually be happy with receiving the non stringified object in writeOut!