-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add support for strict mode #41
base: main
Are you sure you want to change the base?
Conversation
Codecov Report
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. @@ Coverage Diff @@
## main #41 +/- ##
==========================================
+ Coverage 98.78% 99.04% +0.25%
==========================================
Files 1 1
Lines 165 210 +45
Branches 70 81 +11
==========================================
+ Hits 163 208 +45
Misses 2 2
|
- throw for missing string option value - throw for unexpected value for boolean option
10508f8
to
af41dfc
Compare
More detail about interaction of
Two other approaches which I considered:
Due to code refactor to avoid changing non-strict behaviour, currently independent. Still calling |
I’d like to understand any differences from util.parseArgs here - it’d be ideal to match the api as much as possible. |
I am keeping Sort of the inverse of what you asked but easier, demo of current state of this PR and // minimist.js
const parseArgs = require('minimist');
try {
const result = parseArgs(process.argv.slice(2), {
strict: true,
string: ['str'],
boolean: ['bbb']
});
console.log(result);
} catch (err) {
console.log(err.message);
} $ node minimist.js --oops
Unknown option "oops"
$ node minimist.js --str
Missing option value for option "str"
$ node minimist.js --bbb=xyz
Unexpected option value for option "bbb" // parseArgs.js
const { parseArgs } = require('node:util');
try {
const result = parseArgs({
strict: true,
options: {
str: { type: 'string' },
bbb: { type: 'boolean'}
}
});
console.log(result);
} catch (err) {
console.log(err.message);
} $ node parseArgs.js --oops
Unknown option '--oops'
$ node parseArgs.js --str
Option '--str <value>' argument missing
$ node parseArgs.js --bbb=xyz
Option '--bbb' does not take an argument |
I currently have added a |
Minimist does not support validating that the command-line arguments match what the author intended. The default approach is to always parse the command-line arguments, and it is difficult for the author to detect problems. This can lead to a poor end-user experience when unintended usage silently produces unexpected results.
This PR adds
opts.strict
which will throw for these usage errors:See: #37 #39