lukeed/mri

Unknown handling seems wrong

w33ble opened this issue · 2 comments

It's a little odd that the unknown function requires that aliases be defined to match defined flags. It means that exact matches "fail", but because of the way the parser works, prefixed flags do not. Example:

const mri = require('mri')
const opts = { boolean: ['check'], unknown: (arg) => console.log('unknown arg:', arg) }

// this hits the unknown handler
mri(['--check'], opts)

// this just works
const args = mri(['--no-check'], opts)

I'm curious why the alias requirement exists. Why not just continue if there's an exact flag match?

Interesting, thanks! I haven't checked out the code in a while to be honest, but I'll try and make my way back to this sometime this week.

I think what needs to happen is all boolean and string keys need to be written into the alias map on options parsing. 🤔 Will check it out. I do remember that unknown was a last-second type of addition.

Kinda got that from the commits and commented out tests ;)

It’s like a 2 line change. Defaults end up in the map, and you check for aliases already, it’s just a matter of also checking for a match in string or boolean. I wrote some tests and have a fix, I’ll make a pr. Just wasn’t sure if there was a reason alias was required. Sounds like there really isn’t.