Skip to content

passThroughOptions being a superset of enablePositionalOptions #1947

@aweebit

Description

@aweebit
new Command()
  .passThroughOptions()
  .command('sub')
  .passThroughOptions();

Running this code results in the following error:

Error: passThroughOptions can not be used without turning on enablePositionalOptions for 
parent command(s)
    at Command.passThroughOptions (lib/command.js:739:13)

But actually, using passThroughOptions on a subcommand whose parent also uses it should be okay because passThroughOptions makes options positional just like enablePositionalOptions. That is demonstrated by the following snippet

const program = new Command()
  .passThroughOptions()
  .option('-x');
const sub = program.command('sub')
  .option('-x');
program.parse(['sub', '-x'], { from: 'user' });
console.log(program.opts()); // {}
console.log(sub.opts()); // { x: true }

and explained by the fact passThroughOptions is included in the check in lib/command.js, line 1522.

So is there any point in requiring enablePositionalOptions when passThroughOptions which too has the needed effect is already on?

My suggestion is

  • either not to throw the error when the parent command uses passThroughOptions,
  • or to change the semantics for passThroughOptions so that there is no functionality overlap with enablePositionalOptions.

The latter idea introduces a breaking change but could be justified by some absurd use cases like

# --verbose to be passed through due to using passThroughOptions
node program --trace curl --verbose https://github.com/

# --verbose to be consumed by program command due to not using enablePositionalOptions
node program utility-subcommand --verbose

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