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

Bug: different result for -x1 and -1x when -x and -1 are both booleans #283

Closed
b1f6c1c4 opened this issue Jun 11, 2020 · 4 comments · Fixed by #294
Closed

Bug: different result for -x1 and -1x when -x and -1 are both booleans #283

b1f6c1c4 opened this issue Jun 11, 2020 · 4 comments · Fixed by #294

Comments

@b1f6c1c4
Copy link

> require('yargs-parser')(['-x1'], { boolean: ['x', '1'] })
{ _: [], x: false }
> require('yargs-parser')(['-1x'], { boolean: ['x', '1'] })
{ '1': true, _: [], x: true }

What I'm looking for is to both get { '1': true, _: [], x: true }, because -x and -1 are both boolean switches and 1 is NOT just an argument for -x.

@QmarkC
Copy link
Contributor

QmarkC commented Jun 11, 2020

Quick triage, looks like the issue is in L270-L276:

        // current letter is an alphabetic character and next value is a number
        if (/[A-Za-z]/.test(letters[j]) &&
          /^-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
          setArg(letters[j], next)
          broken = true
          break
        }

Adding a check to see if next is a boolean alias seems to fix it and doesn't break any existing tests.

        // current letter is an alphabetic character and next value is a number
        if (/[A-Za-z]/.test(letters[j]) &&
          /^-?\d+(\.\d*)?(e-?\d+)?$/.test(next) &&
          checkAllAliases(next, flags.bools) === false) {
          setArg(letters[j], next)
          broken = true
          break
        }
const parse1 = parser(['-x1'], { boolean: ['x', '1'] })
console.log(parse1)
// { '1': true, _: [], x: true }

const parse2 = parser(['-1x'], { boolean: ['x', '1'] })
console.log(parse2)
// { '1': true, _: [], x: true }

@b1f6c1c4
Copy link
Author

@QmarkC This solution seems good and could you please make a PR?

@QmarkC
Copy link
Contributor

QmarkC commented Jun 12, 2020

@QmarkC This solution seems good and could you please make a PR?

@b1f6c1c4 I can look into this further after the TypeScript refactor #285 is merged.

@b1f6c1c4
Copy link
Author

@QmarkC Hi, could you please have a look at this?

QmarkC added a commit to QmarkC/yargs-parser that referenced this issue Jul 20, 2020
@bcoe bcoe closed this as completed in #294 Aug 5, 2020
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 a pull request may close this issue.

2 participants