yargs / yargs/yargs-parser

Migrating an option value from boolean to choices requires breaking changes

Open
#412 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
519
Forks
129
Avg merge
1h 34m
Merged PRs (30d)
1

Description

Related: yargs/yargs#1599

Boolean options have this special property:

If a non-flag option - unless true or false - follows key in process.argv, that string won’t get set as the value of key.

This means that if a CLI defines an option as a boolean, it isn't currently possible to later change that option to support more choices than true or false without creating a breaking change for the CLI's interface.

In other words, this happens currently:

require('yargs-parser')('--foo bar')
> { _: [], foo: 'bar' }

require('yargs-parser')('--foo bar', { boolean: ['foo'] })
> { _: [ 'bar' ], foo: true }

If the choices option were handled already in the parser, it'd be possible to (optionally?) allow similar behaviour for it:

require('yargs-parser')('--foo bar', {
  choices: ['true', 'false', 'maybe'], // not a current parser option
  default: { foo: 'maybe' }
})
> { _: [ 'bar' ], foo: 'maybe' }

A similar result could also be achieved if the coerce function could indicate that the given value should not be consumed for the current key, but parsed independently:

require('yargs-parser')('--foo bar', {
  coerce: {
    foo(value) {
      if (['true', 'false', 'maybe'].includes(value) return value
      this.doNotEat() // not currently defined; could also be an argument
      return 'maybe'
    }
  }
})
> { _: [ 'bar' ], foo: 'maybe' }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with yargs-parser's boolean-option handling and review the related yargs/yargs#1599 discussion. Compare the current --foo bar behavior with the proposed choices and coerce approaches; done means a defined way to preserve the positional value while allowing boolean options to gain additional choices without a breaking CLI change.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.