yargs / yargs/yargs-parser

Strange implicit camel-case conversion when use both unknown-options-as-args and alias

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

Nobody has claimed this yet.

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

Description

Given code below:

const result = require("yargs-parser")(
  ["--foo", "bar", "--outDir", "dist"],
  {
    string: ["_", "foo", "out-dir"],
    configuration: {
      "unknown-options-as-args": true 
    }
  }
);

console.log(result);

This will print:

{ _: [ '--outDir', 'dist' ], foo: 'bar' }

In which outDir is parsed as a positional argument. This makes sense because outDir is not presented in string, even though its kebab-case variation presented.

However, when I set an alias for out-dir, It behaves in another way:

const result = require("yargs-parser")(
  ["--foo", "bar", "--outDir", "dist"],
  {
    string: ["_", "foo", "out-dir"],
    configuration: {
      "unknown-options-as-args": true 
    },
    alias: {
      "o": "out-dir"
    }
  }
);

console.log(result);

This will print:

{ _: [], foo: 'bar', outDir: 'dist', o: 'dist', 'out-dir': 'dist' }

In which the former unknown option outDir is parsed as a named argument now. I don't know if there are some implicit conversions under the hood?

If the camel-case conversion is intended, I hope the first example will treat outDir as a known named argument too.

Tested in the current version of yargs-parser@20.2.6

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 two yargs-parser@20.2.6 examples from the issue and compare handling of unknown-options-as-args with the alias for out-dir. Trace the parser's alias and camel-case conversion paths to determine why --outDir changes classification. Done means the behavior is documented by a regression test and matches the intended handling of the camel-case option.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.