Hey everyone,
i currently updated to commanderJs 5.0.0 and now some of my command are not working fine when providing the option name.
Given is the following command with the option name:
program.command('test').description('Logs something').option('-n, --name <value>').action(args => console.log(args.name))
If i run test the command works as expected.
But if i run test --name Foo in my console Commander throws the following error:
TypeError: this.name is not a function
at Command._parseCommand (....\node_modules\commander\index.js:910:37)
at Command._dispatchSubcommand (....\node_modules\commander\index.js:860:18)
at Command._parseCommand (....\node_modules\commander\index.js:877:12)
at Command.parse (....\node_modules\commander\index.js:712:10)
I already tried this Avoiding option name clashes
Anyone has any suggestions
name is a method on Command, so you will hit issues with an option --name when using default behaviour of storing the value as a property. It seems to work ok by adopting the new style processing for me?
const { Command } = require('commander');
const program = new Command();
program
.storeOptionsAsProperties(false)
.passCommandToAction(false)
.name('my-name')
.option('--name <value>', 'Enter name')
.action((options) => {
console.log(`Name option is ${options.name}`);
});
program.parse();
$ node index.js
Name option is undefined
$ node index.js --name foo
Name option is foo
$ node . --help
Usage: my-name [options]
Options:
--name <value> Enter name
-h, --help display help for command
Your solution fixed the issue. Thanks for your answer.
Just ran into this one as well. Seems like it would be nice to throw an error if someone specifies a --name option. I can imagine this one being run into a fair bit, name is probably a pretty popular choice.
Opened a PR to add a warning for option name clashes: #1275
Most helpful comment
nameis a method on Command, so you will hit issues with an option--namewhen using default behaviour of storing the value as a property. It seems to work ok by adopting the new style processing for me?