yargs / yargs/yargs-parser

array w/ default breaks requiresArg

Open
#445 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

yargs 17.5.1

when an array option is given a default value, requiresArg no longer works.

example

console.log(
    yargs(['-s'])
        .option('s', { type: 'array', default: ['turtles'], requiresArg: true })
        .argv
);

expected: failure due to not enough args following -s.

actual: success, default value assigned to -s.

more complete example

import yargs from 'yargs';

let SECTION = msg => console.log(`\n======${msg?` ${msg} `:''}======\n`);

// should fail, and does
SECTION('array + requiresArg');
try {
    console.log(
        yargs(['-s'])
            .option('s', { type: 'array', requiresArg: true })
            .exitProcess(false)
            .argv
    );
} catch { }

// should fail, and does
SECTION('string + default + requiresArg');
try {
    console.log(
        yargs(['-s'])
            .option('s', { type: 'string', default: 'turtles', requiresArg: true })
            .exitProcess(false)
            .argv
    );
} catch { }

// should fail, but doesn't
SECTION('array + default + requiresArg (missing arg)');
console.log(
    yargs(['-s'])
        .option('s', { type: 'array', default: ['turtles'], requiresArg: true })
        .argv
);

// should succeed, and does
SECTION('array + default + requiresArg (with arg)');
console.log(
    yargs(['-s', 'stank', 'bread'])
        .option('s', { type: 'array', default: ['turtles'], requiresArg: true })
        .argv
);

output:

====== array + requiresArg ======

Options:
      --help     Show help                                             [boolean]
      --version  Show version number                                   [boolean]
  -s                                                                     [array]

Not enough arguments following: s

====== string + default + requiresArg ======

Options:
      --help     Show help                                             [boolean]
      --version  Show version number                                   [boolean]
  -s                                               [string] [default: "turtles"]

Not enough arguments following: s

====== array + default + requiresArg (missing arg) ======

{ _: [], s: [ 'turtles' ], '$0': 'yargteststuff.js' }

====== array + default + requiresArg (with arg) ======

{ _: [], s: [ 'stank', 'bread' ], '$0': 'yargteststuff.js' }

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 by running the supplied yargs-parser reproduction with an array option, a default value, and requiresArg. Trace the array-option argument handling to see why -s accepts its default without an argument; done means the missing argument fails while the example with provided arguments still succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.