Skip to content

.conflicts for a global options ignored in command, unless explicitly added to command handler #764

@rasmuslp

Description

@rasmuslp

I'm trying to define some options, which should be mutually exclusive, but the .conflicts option specified on top-level seems to be ignored when a command is provided. Is this the intended behaviour?

Examples

v1

// v1: Plain .conflicts
require('yargs')
    .command({
        command: 'a'
    })
    .option('b', {
        global: true
    })
    .option('c', {
        global: true
    })
    .conflicts('b', 'c')
    .argv;

If I run v1 with no command, yargs stop me. But if I add the command, it doesn't:

$ node v1.js -b 2 -c 3
Arguments b and c are mutually exclusive

$ node v1.js a -b 2 -c 3
# No output

v2

// v2: Explicitly added .conflicts to command builder
require('yargs')
    .command({
        command: 'a',
        builder: function (yargs) {
            return yargs
                .conflicts('b', 'c');
        }
    })
    .option('b', {
        global: true
    })
    .option('c', {
        global: true
    })
    .conflicts('b', 'c')
    .argv;

In v2 yargs stops me:

$ node v2.js -b 2 -c 3
Arguments b and c are mutually exclusive

$ node v2.js a -b 2 -c 3
Arguments b and c are mutually exclusive

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions