I've got a working CLI program using the "commander" npm package. Generally it works great.
What I'd like to be able to do is add a command with its own option, and use a single-character abbreviation that also appears as an overall program option. That would mean something like this:
program -a "macintosh" someCommand -a 100
meaning I'd have a configuration like:
import { program } from "commander";
// ...
program
.option("-a, --apple <name>", "type of apple");
program
.command("foo <id>")
.description("do a foo")
.option("-a, --abcde", "the a option for foo")
// ...
When I try that, Commander seems to conflate the "-a" to the overall program with the "-a" I intend to be for the "foo" command. If I give the "--abcde" form, then it works fine. My "global" -a
has an argument (as in the above example), while the one on my "foo" command is a simple boolean.
When I run program
by itself to get the help, it's fine, and if I run program help foo
it shows me the -a
option with the option description specific to the "foo" command.
I realize this may be a deliberate design decision, which is the point of the question.
Short version: program.enablePositionalOptions()
Long version
The default behaviour is that the top level options are global and can appear anywhere on the command line. So by default, the program sees the option and the subcommand does not.
To make the program options positional (i.e. before the subcommand) so you can use the same named option in the subcommand, use .enablePositionalOptions()
.
(Disclaimer: I am a maintainer of Commander.)