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

array w/ default breaks requiresArg #445

Open
JC3 opened this issue Jun 28, 2022 · 1 comment
Open

array w/ default breaks requiresArg #445

JC3 opened this issue Jun 28, 2022 · 1 comment
Labels

Comments

@JC3
Copy link

JC3 commented Jun 28, 2022

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' }
@bcoe bcoe transferred this issue from yargs/yargs Jul 4, 2022
@bcoe
Copy link
Member

bcoe commented Jul 4, 2022

@JC3 thank you for the bug report 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants