Skip to content

Proposals for on/off switch #691

@ntkme

Description

@ntkme

The justification for this has been discussed #108. I have working code but I'd like to open a discussion before opening a PR.

Proposal 1

program
  .version('0.0.1')
  .option('    --[no-]color', 'optionally turn on or off the color');

Proof of concept implementation:

https://github.com/ntkme/commander.js/compare/master...ntkme:optional-switch

Pros

  • Small overhead
  • Default value works as expected

Cons

  • No short flag

Proposal 2

program
  .version('0.0.1')
  .option('-c, --color', 'enable color')
  .option('-C, --no-color', 'disable color')

Proof of concept implementation:

https://github.com/ntkme/commander.js/compare/master...ntkme:boolean-switch

Pros

  • Short flag works fine

Cons

  • The proof of concept implementation has to take a look back at all previous options, which means:
    • Weak performance
    • Default value has to be set on the second option
    • Can be overcome by setting default values only when calling .parse() (breaking change, not a good idea)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions