Skip to content

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

Closed
aweebit wants to merge 1 commit intotj:developfrom
aweebit:feature/command-arguments
Closed

Add missing support for command-arguments to commands added with .addCommand()#1941
aweebit wants to merge 1 commit intotj:developfrom
aweebit:feature/command-arguments

Conversation

@aweebit
Copy link
Copy Markdown
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
Copy Markdown
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