tj / commander.js

node.js command-line interfaces made easy
MIT License
26.79k stars 1.69k forks source link

Using `.passThroughOptions()` without enabling `.enablePositionalOptions()` #2281

Open aryanjassal opened 2 days ago

aryanjassal commented 2 days ago

Is there any way to allow passing options through without enabling positional options globally? Part of my program expects it to be disabled, and is built around that. However, one subcommand needs this functionality, and I cannot selectively enable positional arguments for one command.

program subcmd --opt arg1 arg2 --opt

For subcmd, I need to get all arguments after the first positional argument, like ['arg1', 'arg2', '--opt'], but I also have a --opt flag for the subcommand, so the variadic arguments treat the second --opt as part of the subcommand command, which is not what should happen.

Trying to enable passThroughOptions does not achieve anything as positional arguments are disabled globally. Why do we need to enable positional arguments globally to use passThroughOptions in a subcommand anyways?

shadowspawn commented 2 days ago

Why do we need to enable positional arguments globally to use passThroughOptions in a subcommand anyways?

The default parsing is that options for the root command (or any command) can come before or after subcommands. The parsing looks for global options before it looks for subcommands. So in your example, if the root command also had same --opt option it would consume the trailing opt before finding subcmd.

It would take a rewrite of the parsing to allow the leaf command to change the parsing behaviour in isolation, which was more than I wanted to do to add the feature.

(One idea suggested previously was that defining .passThroughOptions() on the higher level command(s) could be enough, without defining .enablePositionalOptions(). See #1947.)

shadowspawn commented 2 days ago

Part of my program expects it to be disabled, and is built around that.

So to confirm, you use global options after subcommands in other parts of your program? Like:

program subcmd2 --programOption