Skip to content

Inconsistent array parsing when value is passed through config() option #789

Closed

Description

@le0nik

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 Feb 21, 2017

    @bcoe
    Member

    @le0nik I believe this is expected behavior, when passing config, this should be the already parsed values, so you'd pass something like this:

    yargs.config({
      arr: ['foo', 'bar', 'snuh']
    })
    

    Which could be overridden with:

    foo.js --arr foo bar

    Is it wort adding a note to our documentation on this topic?

  2. le0nik commented on Feb 21, 2017

    @le0nik
    Author

    @bcoe the reason I was sure it was going to work is that it works for other types of values. If I pass 'true' and the type of this option is boolean the resulting value's going to be true. Same works with numbers. In my case I've added a preprocessing step before passing config to yarn where I parse array option(if it's value is string).

  3. bcoe commented on Feb 21, 2017

    @bcoe
    Member

    @le0nik valid point, perhaps open an issue on yargs-parser I'd definitely consider supporting this.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions