Skip to content

feat!: populate error if incompatible narg/count or array/count options are used - #191

Merged
bcoe merged 2 commits into
yargs:masterfrom
juergba:issue/count-array
Oct 28, 2019
Merged

feat!: populate error if incompatible narg/count or array/count options are used#191
bcoe merged 2 commits into
yargs:masterfrom
juergba:issue/count-array

Conversation

@juergba

@juergba juergba commented Jul 24, 2019

Copy link
Copy Markdown
Contributor

Array:

var args = parse('-f foo baz -f', {
    array: ['f'],
    count: ['f'],
    configuration: {
      'duplicate-arguments-array': true,
      'flatten-duplicate-arrays': false
    }
})       //  { _: [], f: [ [ [Function: increment], [Function: increment] ], [] ] }

The result is incorrect, it should be: { _: [ 'foo', 'baz' ], f: 2}

Narg:

var args = parse('-f foo baz -f', {
  narg: {'f': 1},
  count: ['f']
})     // { _: [ 'baz' ], f: 1 }

The result is incorrect, it should be: { _: [ 'foo', 'baz' ], f: 2 }

A configuration opts.count plus opts.array or opts.narg does not make sense. Nevertheless this combination can be set by the user.

  • we delete opts.array / opts.narg settings before parsing
  • we populate error with an error object (but don't throw one)

@juergba
juergba force-pushed the issue/count-array branch from 23eeb54 to 7ec05e7 Compare July 25, 2019 07:42
@juergba juergba changed the title Fix: "opts.count" with array typed option Fix: "opts.count" with array typed or narg option Jul 25, 2019
@juergba
juergba marked this pull request as ready for review July 25, 2019 08:30
@bcoe

bcoe commented Jul 29, 2019

Copy link
Copy Markdown
Member

I don't like that we would silently correct strange behavior like this.

I would rather that we potentially add this as a validation check in yargs itself, and raise an exception if incompatible flags are set.

@juergba

juergba commented Jul 29, 2019

Copy link
Copy Markdown
Contributor Author

A validation check in yargs itself does not help if yargs-parser is used independently.

I see two options:

  • we fix incompatible flags before parsing (by deleting them). We wouldn't have to change the parsing logic this way.
  • we add additional checks to the parsing logic (in another PR you are trying to avoid this)

IMO the validation of the user input - in this case the configuration settings - should be done before the actual parsing. Please let me know how to proceed.

There will be more incompatible combinations the more configuration options we have, eg. collect-unknown-options and halt-at-non-option.

@bcoe

bcoe commented Jul 29, 2019

Copy link
Copy Markdown
Member

@juergba we use the error object elsewhere to provide information about why a parse failed:

https://github.com/yargs/yargs-parser/blob/master/index.js#L365

If a user provides configuration that's contradictory, e.g., indicating that something is an array and a counter, I think it might be worthwhile to get in the habit of populating an error object.

We could do this early on in the parse process, as a way of indicating to the user that the parse may have been invalid; I think this would be better than making an effort to correct incompatible config.

@juergba

juergba commented Jul 29, 2019

Copy link
Copy Markdown
Contributor Author

Ok, understood. I will have a look at the error object.
I set back this PR to a lower priority, the fix of this wouldn't be a breaking change anyway.

@bcoe

bcoe commented Jul 29, 2019

Copy link
Copy Markdown
Member

@juergba the other option on my mind, if we did want to make explicit decisions like saying that count takes precedence over array, would be that we eventually write a former grammar for our CLI parser, along these lines ... it would be a lot of work but could be good for the community.

tldr; I think in some cases populating an error is a good call (if the combination of config is silly), but I would love to make an effort to formally define what is "correct" behavior over time. I think this could align with some of @boneskull's interests (he'd like to define a MVP command line parser for Node.js).

@bcoe

bcoe commented Sep 6, 2019

Copy link
Copy Markdown
Member

@juergba where did you land on this one, still something you'd like to see over the finish line? what do you think of populating an error?

@juergba

juergba commented Sep 8, 2019

Copy link
Copy Markdown
Contributor Author

Populating an error is a good option. I can imagine many silly combinations of configuration settings, which can be handled that way adding case by case.
I haven't set a time line for this one. You can take over if you need it in your next release. Otherwise I will implement it later when I have more time.

@bcoe

bcoe commented Sep 9, 2019

Copy link
Copy Markdown
Member

@juergba sounds good, as long as we feel we have an approach hammered out, no rush.

@juergba juergba closed this Oct 17, 2019
@juergba juergba reopened this Oct 17, 2019
@juergba

juergba commented Oct 17, 2019

Copy link
Copy Markdown
Contributor Author

@bcoe I applied some changes, please have a look and tell me wether this is the way to go.

  • error contains only one error object, in case of multiple errors the additional errors will be lost.
  • I declared checkConfiguration() within the parse() function in order to have all variables in the scope. checkConfiguration() is also supposed to be extended in the future. Some functions as eg. combineAliases() has been declared outside of it.

@bcoe bcoe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach, I think we'll gradually want to start fleshing it out into a more generic approach; one option would be that we create a configuration object in in index.js that allows us to describe inconsistencies between different config settings, e.g.,:

const configChecks = {
  counts: ['array', 'foo'],
  array: ['bar']
}

I'm comfortable with landing this as a first step though.

@juergba

juergba commented Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Ok, I added two tests.

@bcoe bcoe changed the title Fix: "opts.count" with array typed or narg option feat!: throw exception if incompatible narg/count or narg/array options are used Oct 28, 2019
@bcoe
bcoe merged commit f9a3e4e into yargs:master Oct 28, 2019
@juergba

juergba commented Oct 29, 2019

Copy link
Copy Markdown
Contributor Author

@bcoe I'm not happy with this PR's title. We populate error, but don't throw an exception, and the incompatible options are: count/array and count/narg.

@juergba
juergba deleted the issue/count-array branch October 29, 2019 07:49
bcoe pushed a commit that referenced this pull request Oct 29, 2019
@bcoe bcoe changed the title feat!: throw exception if incompatible narg/count or narg/array options are used feat!: populate error if incompatible narg/count or array/count options are used Oct 29, 2019
@bcoe

bcoe commented Oct 29, 2019

Copy link
Copy Markdown
Member

fixed.

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

Successfully merging this pull request may close these issues.

2 participants