-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
restrict the number of allowed options/strict mode #37
Comments
I'm not sure what you mean - the |
yes, but how do you tell minimist that
so say if I have --min=12 --dry-run=dummy --mode --extra=bling and options= {boolean:["dry-run"], string: ["mode"]} if there an helper to make it easy to flag that:
right now, I don't know how to handle integer params (they will always be pushed to my unkown function), and if there is type error, it's silently dropped, isn't it? Basically, minimist is awesome for quick prototyping, but once I'm clear about which parameters are expected, I'd like to "freeze" that list and display errors when the user puts unexpected parameters (eg either wrong types or unknown params) |
Your Since dry-run is marked as a boolean, I'd expect it would be pretty trivial to validate that for yourself after parsing is completed? |
Minimist does not have much in the way of "strict" support. The One way of getting an integer option to at least be recognised is to give it an alias. Given this code: var argv = require('minimist')(process.argv.slice(2), {
boolean:["dry-run"],
string: ["mode"],
alias: {min: 'm'},
unknown: (unknownParam) => {
console.log({unknownParam});
}
});
console.log(argv); I see this output for your example line: % node index.js --min=12 --dry-run=dummy --mode --extra=bling
{ unknownParam: '--extra=bling' }
{ _: [], 'dry-run': true, min: 12, m: 12, mode: '', extra: 'bling' } |
You might like to take a look at https://nodejs.org/dist/latest-v18.x/docs/api/util.html#utilparseargsconfig
I can do an example program matching your example if you would like to see one. |
nice trick to use alias! this is what I have so far
to make it more compact, ideally:
|
thanks for the pointer, I completely missed that one! |
parseArgs existence, however, doesn’t mean minimist can’t be improved :-) this is still worth adding. |
I raised the idea of a strict mode at the end of #39 (comment) |
Hi,
I'm not sure what's the best place to discuss feature, feel free to close and direct me to a better place
Some users of our cli mistype an argument name (eg the cli expects --dry-run and they type --dryrun).
If would be useful to have a strict mode that stops the process if there is an unknown error
(eg. invalid parameter "dryrun")
I tried to implement it using unknown
however,
X+
The text was updated successfully, but these errors were encountered: