Skip to content

Main command and subcommands have different default values #2439

@Fd929c2CE5fA

Description

@Fd929c2CE5fA
program.version("")
	.option("-s, --size", "size", "1")
	.action(() => {});
program.command("b")
	.option("-s, --size", "size", "5")
	.action((options, command) => {
		console.log(command.optsWithGlobals()); // method A
		console.log({options, ...program.opts()}); // method B
		console.log({...program.opts(), options}); // method C
	});

When a main command and a subcommand have the same options, they are consumed by the main command and never passed to the subcommand.

I know that we can generally use command.optsWithGlobals() to combine the results, but if both have different default values, the main command default value is always used when no parameters are given. This means the subcommand default value is ignored. That is, the result of optsWithGlobals() is similar to method B.

But if method C is used, user input is always overwritten by the subcommand default value.

My expectation are:

  • Define the same option in the main command and subcommand but with different default values
  • Print different default values ​​when using command -h and command b -h
  • Use the default values ​​of each command if the user doesn't specify a size option
  • Always use the user's input when user have specifying the size option

Currently there doesn't seem to be a default or elegant way to achieve this.
I was able to get it to work by just doing custom processing in .hook("preAction".
But I think this might be an issue that should be noticed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions