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

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

Merged
merged 2 commits into from Oct 28, 2019

Conversation

juergba
Copy link
Contributor

@juergba juergba commented Jul 24, 2019

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 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
Copy link
Member

bcoe commented Jul 29, 2019

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
Copy link
Contributor Author

juergba commented Jul 29, 2019

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
Copy link
Member

bcoe commented Jul 29, 2019

@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
Copy link
Contributor Author

juergba commented Jul 29, 2019

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
Copy link
Member

bcoe commented Jul 29, 2019

@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
Copy link
Member

bcoe commented Sep 6, 2019

@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
Copy link
Contributor Author

juergba commented Sep 8, 2019

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
Copy link
Member

bcoe commented Sep 9, 2019

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

@juergba
Copy link
Contributor Author

juergba commented Oct 17, 2019

@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.

Copy link
Member

@bcoe bcoe left a comment

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
Copy link
Contributor Author

juergba commented Oct 28, 2019

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
Copy link
Contributor Author

juergba commented Oct 29, 2019

@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
Copy link
Member

bcoe commented Oct 29, 2019

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.

None yet

2 participants