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

Make strict() work with options containing hyphens #1414

Merged
merged 3 commits into from Aug 30, 2019

Conversation

andrewdotn
Copy link
Contributor

test/usage.js Outdated
@@ -810,6 +810,15 @@ describe('usage tests', () => {
r.should.have.property('exit').and.equal(true)
})

it('fails when an invalid argument is provided with automatic camel case', (done) => {
return yargs('--foo-bar')
Copy link
Member

Choose a reason for hiding this comment

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

prior to your fix would the following also fail?

yargs('--foo-bar')
        .option('foo-bar', {describe: 'foo bar option'})
        .strict()
        .fail((msg) => {
          return done()
        })
        .argv

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m not sure why you’re asking about that, but both before and after my fix, with that code, argv returns {'foo-bar': true, 'fooBar': true} instead of failing.

@bcoe
Copy link
Member

bcoe commented Aug 26, 2019

thanks for the contribution @andrewdotn, I think we should add a couple more test cases for the happy paths, but this looks like it's on the right track.

@@ -96,7 +96,7 @@ module.exports = function validation (yargs, usage, y18n) {
if (specialKeys.indexOf(key) === -1 &&
!positionalMap.hasOwnProperty(key) &&
!yargs._getParseContext().hasOwnProperty(key) &&
!aliases.hasOwnProperty(key)
!self.isValidAndNotMadeUpAlias(key, aliases)
Copy link
Member

Choose a reason for hiding this comment

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

let's call this, isValidAndNotNewAlias, just to match naming.

@andrewdotn
Copy link
Contributor Author

Hello @bcoe, I’ve renamed that method and added some more tests. Anything more to get this merged?

@bcoe bcoe merged commit b774b5e into yargs:master Aug 30, 2019
@andrewdotn
Copy link
Contributor Author

@bcoe Thanks! The Fixes: in the PR description seems to have done the auto-close-on-commit thing for only the first issue, so you may want to close #1171 #1236 #1325 as well.

@bcoe
Copy link
Member

bcoe commented Sep 6, 2019

@andrewdotn could you please give the following a try:

npm i yargs@next

And let me know if everything is working as expected?

@andrewdotn
Copy link
Contributor Author

@bcoe Yup, confirmed working, thanks!

Test file:

const yargs = require('yargs');

const argv = yargs
.strict()
.argv;

console.log(argv);

Before:

$ node foo.js --foo-bar
{ _: [], 'foo-bar': true, fooBar: true, '$0': 'foo.js' }

yargs@next:

h$ node foo.js --foo-bar
Options:
--help     Show help                                                 [boolean]
--version  Show version number                                       [boolean]

Unknown arguments: foo-bar, fooBar
Returned 1.

@bcoe
Copy link
Member

bcoe commented Oct 7, 2019

@andrewdotn sorry that it took a while to get a release out the door; this work should now be available in yargs@14.2.0.

Let me know if you bump into any issues, and thank you for your contribution!

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.

Hyphenated option breaks strict validation
3 participants