Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: this.name is not a function #1226

Closed
nicogenz opened this issue Mar 22, 2020 · 4 comments
Closed

TypeError: this.name is not a function #1226

nicogenz opened this issue Mar 22, 2020 · 4 comments

Comments

@nicogenz
Copy link

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

@shadowspawn
Copy link
Collaborator

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

@nicogenz
Copy link
Author

Your solution fixed the issue. Thanks for your answer.

@counterbeing
Copy link

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.

@shadowspawn
Copy link
Collaborator

Opened a PR to add a warning for option name clashes: #1275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants