Skip to content

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

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

Open
Xunnamius opened this issue Mar 16, 2025 · 1 comment · May be fixed by #502
Open

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

Xunnamius opened this issue Mar 16, 2025 · 1 comment · May be fixed by #502
Labels

Comments

@Xunnamius
Copy link

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': ''
}
@shadowspawn
Copy link
Member

I reproduced the issue using your example, and agree that looks like the fix.

Xunnamius added a commit to Xunnamius/yargs-parser that referenced this issue Mar 17, 2025
Xunnamius added a commit to Xunnamius/yargs-parser that referenced this issue Mar 17, 2025
Xunnamius added a commit to Xunnamius/yargs-parser that referenced this issue Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants