Skip to content
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

defaultValue for type Array null #24

Closed
Townsheriff opened this issue Sep 13, 2016 · 3 comments
Closed

defaultValue for type Array null #24

Townsheriff opened this issue Sep 13, 2016 · 3 comments

Comments

@Townsheriff
Copy link

Fails to parse command line if defaultValue is null and type is String and multiple is true.

Using following options:

    {
        name: 'tags',
        alias: 't',
        type: String,
        multiple: true,
        defaultValue: null
    },

If --tags array values are provided in CLI then it parses them incorrectly. Last string is split in array of chars and everything else is ignored.

@75lb
Copy link
Owner

75lb commented Sep 16, 2016

hi.. this is a rare use case - by specifying multiple: true you are telling command-line-args to return an array of zero or more values.. you don't need to specify defaultValue on a multiple, because you will always get an array back.

Still, i will look at this and potentially add some validation to prevent incorrect usage..

So, you want options.tags to return null (if no --tags were supplied) or [ 'tag1', 'tag2' etc ] if --tags tag1 tag2 etc was supplied, correct?

@Townsheriff
Copy link
Author

Hi, no, it was a bug in my code and unexpected behaviour from package. Being a good person I reported it.
If no tags was supplies --tags then options.tags was null, but if --tags tag1 tag2 then options.tags was ['t', 'a', 'g', '2']

@75lb 75lb closed this as completed in c343257 Jan 19, 2017
@75lb
Copy link
Owner

75lb commented Jan 19, 2017

was unable to reproduce your particular ['t', 'a', 'g', '2'] output but did find a fix a bug..

the intended behaviour is that if multiple: true is set, you are guaranteed an array back in the output.. So, if your option definition is

{
  name: 'tags',
  alias: 't',
  type: String,
  multiple: true,
  defaultValue: null
},

and you did not pass a value for --tags on the command line, then your output would be

{
  tags: [ null ]
}

i.e. your default value as an array, as multiple: true guarantees an array.

You would get the same output if your definition looked like this:

{
  name: 'tags',
  alias: 't',
  type: String,
  multiple: true,
  defaultValue: [ null ]
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants