Skip to content

String values in config files that contain embedded quotes are not handled correctly #385

Open

Description

@jbms

If a config object specified via the .config option to yargs contains a value that starts and ends with quotes, yargs strips those quotes, due to this code here:

// strings may be quoted, clean this up as we assign values.

That seems like confusing, and undesirable behavior, and makes it difficult to pass values containing embedded quotes.

For example:

console.log(require('yargs').config({a: JSON.stringify('hello')}).parse());

This prints:

{ _: [], a: 'hello', '$0': 'yargs-bug.js' }

But I would expect it to instead print:

{ _: [], a: '"hello"', '$0': 'yargs-bug.js' }

I would suggest that when parsing already-split arguments, e.g. from process.argv, or when processing a config file, quotes should never be stripped, and no additional splitting should ever occur. The only time stripping of quotes should happen is when parsing an unsplit string. In that case, the string should be parsed into an array of strings using shell-style parsing, and then parsing should proceed using the resultant array, and do no further splitting or quote handling.

Activity

  1. rogatec commented on Mar 23, 2022

    @rogatec

    Are there any news how to solve this?
    I have a quite similar issue - via the config method I am reading a JSON file, which has some properties with dashes, for e.g. "X-MyPropety": "1",

    Those properties will be duplicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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