Skip to content

Incorrect parsing of unknown args with dashes when they resemble known args #501

Open
@Xunnamius

Description

@Xunnamius

I have a command that calls other commands, and accepts an --options flag that can pass through arguments to these other commands; this requires unknown-options-as-args to be enabled. Some of these arguments have dashes in them. It seems yargs-parser will incorrectly parse unknown arguments with dashes in them when they resemble known arguments.

This is fixed by updating line 987 to:

//const flagEndingInNonWordCharacters = /^-+([^=]+?)\W+.*$/
const flagEndingInNonWordCharacters = /^-+([^=]+?)[^\w-]+.*$/

I can open a PR if there's interest.

Example:

const cli = require('yargs/yargs')();

cli.command(
  '$0',
  'description',
  (y) => {
    y.parserConfiguration({ 'unknown-options-as-args': true });

    return y.options({
      'script-options': {
        alias: 'options',
        array: true,
        default: []
      },
      'skip-packages': {
        alias: ['skip', 'skip-package'],
        string: true,
        array: true,
        default: []
      }
    });
  },
  (argv) => console.log('called')
);

// Correct behavior
cli.parseSync(['--options', '--task', '7'])

{
  _: [],
  options: [ '--task', 7 ],
  'script-options': [ '--task', 7 ],
  scriptOptions: [ '--task', 7 ],
  'skip-packages': [],
  skip: [],
  'skip-package': [],
  skipPackage: [],
  skipPackages: [],
  '$0': ''
}

// Wrong behavior
cli.parseSync(['--options', '--skip-task', '7'])

{
  _: [],
  options: [],
  'script-options': [],
  scriptOptions: [],
  'skip-task': 7,
  skipTask: 7,
  'skip-packages': [],
  skip: [],
  'skip-package': [],
  skipPackage: [],
  skipPackages: [],
  '$0': ''
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions