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

Question about error call .storeOptionsAsProperties() before adding options #1434

Closed
johannchopin opened this issue Jan 12, 2021 · 2 comments

Comments

@johannchopin
Copy link

johannchopin commented Jan 12, 2021

i've written this tiny cli with the option port:

  program
    .version('1.0.0', '-v, --version', 'output the current version')
    .option('-p, --port <number>', 'port to serve Restapify instance')

Sadly it seems to conflict with a property on Command:

    option 'port' clashes with existing property 'port' on Command
    - call storeOptionsAsProperties(false) to store option values safely,
    - or call storeOptionsAsProperties(true) to suppress this check,
    - or change option name

So I made this change like mentioned here:

  program
    .storeOptionsAsProperties(false)
    .passCommandToAction(false)

  program
    .version('1.0.0', '-v, --version', 'output the current version')
    .option('-p, --port <number>', 'port to serve Restapify instance')

But then I get this curious error which disturb me a bit because I call it before defining options:

call .storeOptionsAsProperties() before adding options

Has someone an idea what I'm doing wrong here?

@shadowspawn
Copy link
Collaborator

The way the options are stored by default is changing in Commander 7 because of the potential for conflicts with existing properties. See #1229 for some history.

However, I can not reproduce either the port conflict or the error to call .storeOptionsAsProperties() before adding options with your program fragment.

Are you using the global program rather than creating a new Command? I suspect some other piece of code is also using the global, and adding a port option with a default value!

I expect the behaviour will change if you create a new Command, but you might want to track down the source of the conflict too:

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

@johannchopin
Copy link
Author

Thanks @shadowspawn you are right I was just using import { program } from 'commander'. With the instantiation of the Command class it's working like a charm 👍 I just figured that it was well mentioned in the README to instantiate this class so sorry to waste you time 😅

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