pkgjs / parseargs

Polyfill of `util.parseArgs()`
Apache License 2.0
121 stars 9 forks source link

feat: Add strict mode to parser #74

Closed aaronccasanova closed 2 years ago

aaronccasanova commented 2 years ago

This PR introduces a strict mode parser config that is enabled by default.

Errors on:

Examples:

// node unknown-option.js --foo --bar
parseArgs({
  strict: true,
  options: { foo: { type: 'boolean' } },
});
// [Error]: Unknown option '--bar'
// node invalid-string-option.js --foo
parseArgs({
  strict: true,
  options: { foo: { short: 'f', type: 'string' } },
})
// [Error]: Option '-f, --foo <value>' argument missing
// node invalid-boolean-option.js --foo=bar
parseArgs({
  strict: true,
  options: { foo: { type: 'boolean' } },
})
// [Error]: Option '--foo' does not take an argument
aaronccasanova commented 2 years ago

Converted to draft until we get some consensus in #11

bcoe commented 2 years ago

@aaronccasanova bother you for a rebase?

bcoe commented 2 years ago

@aaronccasanova landed @shadowspawn's refactor this morning, so I think this and @bakkot's potential refactor around positionals are the last two blockers for MVP.

shadowspawn commented 2 years ago

In Commander, the error messages refer to the option with both the short and long form and the argument if applicable, rather than just what was used on the command line. So in same style as the usage might appear in the help. If the user used the short form, the long form is more informative about the option purpose.

% fab clone --branch
error: option '-b, --branch <branchname>' argument missing
% fab --silly
error: unknown option '--silly'
aaronccasanova commented 2 years ago

Thanks for the reviews and feedback! I'm happy for this PR to be merged as is and willing to help with any quick follow ups. My last PR was merged by @shadowspawn and I'm not entirely sure if I should be merging into main. That being said, I don't want to block anyone, so feel free to merge and/or push up any updates!

shadowspawn commented 2 years ago

Taking a last look now. Thanks for all the hard work @aaronccasanova