Skip to content
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

The configureOutput method does not affect to command which added with addCommand method. #1778

Closed
yankos opened this issue Aug 26, 2022 · 2 comments
Labels
docs README (or other docs) could be improved

Comments

@yankos
Copy link

yankos commented Aug 26, 2022

I want write all output with winston module. I trying to add the configureOutput method before or after all commands. It affects only command which addded with the command method. Live example

...
// Init commander.
const program = new Command()
    .name("CLI")
    .configureOutput({
        writeOut: (str) => {
            console.log("writeOut:", str);
        },
        writeErr: (str) => {
            console.log("writeErr:", str);
            console.log("Test passed!");
        },
    });

// Add one command, main way.
const command_test_as_var = program.createCommand("test_as_var");
command_test_as_var.addOption(
    new Option("--arg <arg>", "Some word.").makeOptionMandatory()
)
command_test_as_var.action(action);
program.addCommand(command_test_as_var);

// Add second command.
program
    .command("test_inline")
    .addOption(
        new Option("--arg <arg>", "Some word.",).makeOptionMandatory()
    )
    .action(action);
...

I have:

~/Commander-Async-Issue$ node ./output_issue.js test_as_var
error: required option '--arg <arg>' not specified
~/Commander-Async-Issue$ node ./output_issue.js test_inline
writeErr: error: required option '--arg <arg>' not specified

Test passed!

Expecting:

~/Commander-Async-Issue$ node ./output_issue.js test_as_var
writeErr: error: required option '--arg <arg>' not specified

Test passed!
~/Commander-Async-Issue$ node ./output_issue.js test_inline
writeErr: error: required option '--arg <arg>' not specified

Test passed!

I can call configureOutput for each command and then everything works as expected, but according to #1770 I am thinking this is a bug.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Aug 26, 2022

For safety, .addCommand() does not copy the inherited settings from the parent command in case they were written independently with different expectations about the settings and the settings break the added command.

There is a helper routine .copyInheritedSettings() for copying the settings when it is wanted:

program.addCommand(command_test_as_var);
command_test_as_var.copyInheritedSettings(program);

@shadowspawn shadowspawn added the docs README (or other docs) could be improved label Aug 27, 2022
@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Sep 8, 2022
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Oct 2, 2022
@shadowspawn
Copy link
Collaborator

Added mention to README in Commander v9.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs README (or other docs) could be improved
Projects
None yet
Development

No branches or pull requests

2 participants