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

Cannot have option named parent #1212

Closed
Ochaun opened this issue Mar 11, 2020 · 4 comments
Closed

Cannot have option named parent #1212

Ochaun opened this issue Mar 11, 2020 · 4 comments

Comments

@Ochaun
Copy link

Ochaun commented Mar 11, 2020

Summary

Cannot seem to add an option named parent.

Version

5.0.0-4

Code snippet

const program = new commander.Command();

program
  .description('overall program')
  .option('-v, --verbose', 'output everything');

program
  .command('doit <arg0> <arg1> [arg2]')
  .description('bugfix program')
  .option('-p, --parent <parent_name>', 'add parent option','default') //Bug
  .action(async (arg0, arg1, arg2) => {
    console.log("PROGRAM",program.opts().parent)
   });

Run cmd

foo doit -h

Stack Trace

$ foo doit -h
/home/OEMarshall/github/foo/node_modules/commander/index.js:1451
      parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
                                 ^

TypeError: parentCmd.name is not a function
@shadowspawn
Copy link
Collaborator

shadowspawn commented Mar 11, 2020

By default the option values are stored as properties on the command, and there is already an internal property called parent. Related historical issues collected in: #933

The good news is the README has a section on a way to resolve this: 🎉 https://github.com/tj/commander.js#avoiding-option-name-clashes

In particular:

.storeOptionsAsProperties(false)

@shadowspawn
Copy link
Collaborator

For example:

const { Command } = require('commander');
const program = new Command();

program
  .storeOptionsAsProperties(false)
  .passCommandToAction(false);

program
  .command('doit <arg0> <arg1> [arg2]')
  .option('-p, --parent <parent_name>', 'add parent option', 'default')
  .action(async (arg0, arg1, arg2, options) => {
    console.log('PROGRAM', options.parent)
  });

program.parse(process.argv);
$ node index.js doit a b c             
PROGRAM default
$ node index.js doit a b c --parent ppp
PROGRAM ppp

@Ochaun
Copy link
Author

Ochaun commented Mar 12, 2020

It works. Thank you for helping me through this. Closing issue.

@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

2 participants