-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
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 -handcommand b -h - Use the default values of each command if the user doesn't specify a
sizeoption - Always use the user's input when user have specifying the
sizeoption
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.