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

On and off switches are not working #108

Closed
dignifiedquire opened this issue Dec 7, 2012 · 11 comments
Closed

On and off switches are not working #108

dignifiedquire opened this issue Dec 7, 2012 · 11 comments

Comments

@dignifiedquire
Copy link

If I try

program.command('start')
  .option('--auto-watch', 'Description')
  .option('--no-auto-watch', 'Description')
  .action(function(options) {
    console.log(options.autoWatch);
  });

I get the following output:

$ node program.js start --auto-watch
false
$ node program.js start --no-auto-watch
false
$ node program.js start
true

That should not happen. I need the first one to be true. Also if I try to set a default value of true that won't work either.

@tj
Copy link
Owner

tj commented Dec 7, 2012

hmm does having both make sense? The logic I have for boolean flags is that if you have --no- it defaults to true, otherwise it defaults to false, having both seems a little odd but let me know

@dignifiedquire
Copy link
Author

The reason for that is that these switches are used to overwrite a configuration file. Depending on what is in the configuration file we need to be able to turn the option on or off.

@tj
Copy link
Owner

tj commented Dec 7, 2012

ah I see, yeah that's a little tricky with the defaults I guess hmm

@dignifiedquire
Copy link
Author

What about the following

.option('--auto-watch', 'Description')

generates two options, one --auto-watch and one --no-auto-watch with the expected behaviour.
(Maybe some flag here to indicate that one wants the --no option to be generated.)
And if one uses

.option('--no-auto-watch', 'Description')

alone it just behaves as it does right now.

@yanickrochon
Copy link

I have a problem with the logic here

.option('-a, --active', 'set as active', Boolean)

sets args.active == false is specified, and undefined if the flag is not specified.

My logic would be that

.option('-a, --active', 'set as active', Boolean)

would set args.active to true if specified, or false (or undefined) otherwise. While

.option('-A, --no-active', 'set as inactive', Boolean)   // or `--not-active` should be valid, too

would set args.active to false if specified, or true otherwise.

I understand that there should be some way to find if the option was specified or not, but I don't think there's any way to tell that such argument is optional or should be always set.

But, again, specifying both would solve the problem.

.option('-a, --active', 'set as active', Boolean)
.option('-A, --no-active', 'set as inactive', Boolean)   // '--not-active'

would check for these rules :

  • if -a / --active is specified, set args.active = true;
  • if -A / --no-active is specified and !args.active, set args.active = false;
  • if option -A / --no-active is declared, setargs.active = true;`
  • otherwise, leave undefined

@lydell
Copy link

lydell commented May 4, 2014

Hehe, even mocha needs this. It has both --colors and --no-colors. Currently mocha uses an ugly hack to achieve the desired results:

// --no-colors

if (!program.colors) mocha.useColors(false);

// --colors

if (~process.argv.indexOf('--colors') ||
    ~process.argv.indexOf('-c')) {
  mocha.useColors(true);
}

(That hack is buggy, too, since -ac won’t work, only -a -c.)

@adius
Copy link

adius commented May 18, 2015

Any progress on this one?

stovmascript added a commit to stovmascript/react-native-version that referenced this issue Sep 5, 2016
@akdor1154
Copy link

Still hitting this in 2017..

@usmonster
Copy link
Contributor

I have an open PR that should fix this: #795. Please review and comment!

@shadowspawn
Copy link
Collaborator

This issue will be resolved when v3.0.0 is released. Available now as a prerelease. See #1001

@shadowspawn
Copy link
Collaborator

Support for combined --foo and --no-foo has shipped in v3 (thanks @usmonster): https://github.com/tj/commander.js/releases/tag/v3.0.0

Closing second oldest open issue. Woop!

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

8 participants