-
Notifications
You must be signed in to change notification settings - Fork 2.3k
chore(cli): **breaking change** throw errors on unknown flags #3921
Conversation
c6a5ad9
to
773208c
Compare
Test? |
Yes, tests please! 😛 |
value?: string; | ||
} | ||
|
||
let describes: Option[] = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you use an object instead of an array of Option
, you can really simplify the code at the end
6373e9e
to
fa11906
Compare
} | ||
let found = false; | ||
for (let optimistKey of Object.keys(optimistOptions)) { | ||
for (let optionKey of Object.keys(optimistOptions[optimistKey])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace innermost loop with found = found || (optimistOptions[optimistKey][key] !== undefined)
fa11906
to
533576a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More minor changes 😄
I like the test though!
|
||
let argv: any = optimist.parse(args); | ||
|
||
// Check to see if additional flags were used. | ||
let unknownKeys: string[] = []; | ||
for (let key of Object.keys(argv).slice(2)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Array.prototype.filter
instead of rolling your own version
// Check to see if additional flags were used. | ||
let unknownKeys: string[] = []; | ||
for (let key of Object.keys(argv).slice(2)) { | ||
if (key === '$0') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather you just initialized found
to key !== '$0'
, since this check is pretty fast.
e352cb5
to
ba752cb
Compare
|
||
// Check to see if additional flags were used. | ||
let unknownKeys: string[] = []; | ||
Object.keys(argv) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noooo..... Ok, I can see where the miscommunication happened. Here's what I was trying to say:
let unknownKeys = Object.keys(argv).slice(2).filter((key) => {
let found = element !== '$0';
for (let optimistKey of Object.keys(optimistOptions)) {
found = found || (optimistOptions[optimistKey][currentValue] !== undefined);
}
return found
});
d27d587
to
0537599
Compare
0537599
to
0a1f77d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once the tests pass. Don't forget to make an issue as well
0a1f77d
to
37363fd
Compare
@@ -84,6 +175,17 @@ if (argv.version) { | |||
process.exit(0); | |||
} | |||
|
|||
if (!argv.disableChecks) { | |||
// Check to see if additional flags were used. | |||
let unknownKeys: string[] = Object.keys(argv).filter((element: string) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'm not gonna make you do anymore with this, but it just occurred to me that Array.prototype.some
is designed exactly for this situation:
let hasUnknownKeys = Object.keys(argv).some((element: string) => {
return element !== '$0' && element !== '_' && allowedNames.indexOf(element) === -1;
});
if (hasUnknownKeys) {
throw new Error('Found extra flags: ' + unknownKeys.join(', '));
}
It's so beautiful)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So beautiful...and.....merging.
clsoes #3216