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

Add missing support for command-arguments to commands added with .addCommand() #1941

Closed
wants to merge 1 commit into from

Conversation

aweebit
Copy link
Contributor

@aweebit aweebit commented Aug 5, 2023

Problem

const { Command } = require('commander');

{
  const program = new Command();
  program.command('sub <arg>').action(arg => console.log(arg));
  program.parse(['sub', 'arg'], { from: 'user' }); // arg
}

{
  const program = new Command();
  const sub = new Command('sub <arg>').action(arg => console.log(arg));
  program.addCommand(sub);
  program.parse(['sub', 'arg'], { from: 'user' }); // error: unknown command 'sub'
}

Solution

Move declared command-argument parsing from .command() to constructor.

ChangeLog

Fixed

  • added missing support for command-arguments to commands added with .addCommand()

@aweebit aweebit changed the title Move declared command-argument parsing to constructor Add support for command-arguments to commands added with .addCommand() Aug 5, 2023
@aweebit aweebit changed the title Add support for command-arguments to commands added with .addCommand() Add missing support for command-arguments to commands added with .addCommand() Aug 5, 2023
@shadowspawn
Copy link
Collaborator

I was considering this PR and the related changes, and realised I do not want to change the parameter to createCommand() to include args. Working backwards, I do not want to change the Command constructor.

This is a long-standing behaviour, and is now somewhat less of an issue. The more recent and now somewhat preferred way of adding arguments is using .argument(), which is also the way to add an argument description if desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants