Skip to content

Inconsistent array parsing(string values are wrapped in an array instead of parsed) #82

Closed

Description

@le0nik

Original issue: yargs/yargs#789.

The string values are parsed correctly in case of boolean or number values, but are not in case of array. Something like https://github.com/mccormicka/string-argv could be used to parse strings into arrays(simple parsing might not work because quotes must be considered, too).

Passed through cli:

const argv = require('yargs')
  .options({
    arr: {
      type: 'array',
    },
  })
  .argv;

console.log('argv', argv);

$ node ./test.js --arr foo bar
Outputs: argv { _: [], arr: [ 'foo', 'bar' ], '$0': 'test.js' }

Passed through .config()

const argv = require('yargs')
  .config({
    arr: 'foo bar',
  })
  .options({
    arr: {
      type: 'array',
    },
  })
  .argv;

console.log('argv', argv);

$ node ./test.js
Outputs: argv { _: [], arr: [ 'foo bar' ], '$0': 'test.js' }

[email protected]

Activity

  1. bcoe commented on Mar 22, 2017

    @bcoe
    Member

    @le0nik thanks for opening this issue; we're playing catch up right now, but will work on getting to this ASAP!

  2. JaKXz commented on Apr 15, 2017

    @JaKXz
    Member

    Good news! I have a failing test around this (I think). PR coming soon!

  3. bcoe commented on Jan 1, 2018

    @bcoe
    Member

    @le0nik @JaKXz having revisited this, my feeling is that this behavior is actually reasonable -- although we may want to document it somewhere?

    My expectation with .config is that that the object passed would already have split arrays and objects out, so something like this:

    .config({
      foo: ["a", "b"],
      bar: {
         a: 99,
         b: 33
       }
    })

    This makes it easy to load a JSON configuration file of disk and use it to populate yargs. tldr; I don't think we should think of the values passed to .config as unparsed command line values, rather they're an intermediate JSON representation.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions