-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
util: support 'option.required' in parseArgs
#44565
Changes from 4 commits
a8c6229
4a68e2f
9d10aa9
a458309
d99ea87
6579230
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1031,6 +1031,9 @@ added: | |
- v18.3.0 | ||
- v16.17.0 | ||
changes: | ||
- version: REPLACEME | ||
pr-url: https://github.com/nodejs/node/pull/44565 | ||
description: support `required` field in option. | ||
- version: | ||
- v18.7.0 | ||
- v16.17.0 | ||
|
@@ -1053,6 +1056,7 @@ changes: | |
times. If `true`, all values will be collected in an array. If | ||
`false`, values for the option are last-wins. **Default:** `false`. | ||
* `short` {string} A single character alias for the option. | ||
* `required` {boolean} Whether this option is required. **Default:** `false`. | ||
* `strict` {boolean} Should an error be thrown when unknown arguments | ||
are encountered, or when arguments are passed that do not match the | ||
`type` configured in `options`. | ||
|
@@ -1085,7 +1089,8 @@ const options = { | |
short: 'f' | ||
}, | ||
bar: { | ||
type: 'string' | ||
type: 'string', | ||
required: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest not including |
||
} | ||
}; | ||
const { | ||
|
@@ -1105,7 +1110,8 @@ const options = { | |
short: 'f' | ||
}, | ||
bar: { | ||
type: 'string' | ||
type: 'string', | ||
required: true | ||
} | ||
}; | ||
const { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -44,6 +44,7 @@ const { | |||||
ERR_PARSE_ARGS_INVALID_OPTION_VALUE, | ||||||
ERR_PARSE_ARGS_UNKNOWN_OPTION, | ||||||
ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL, | ||||||
ERR_PARSE_ARGS_REQUIRED_OPTION | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not describe why it is an error. I suggest say
himself65 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}, | ||||||
} = require('internal/errors'); | ||||||
|
||||||
|
@@ -336,6 +337,16 @@ const parseArgs = (config = kEmptyObject) => { | |||||
} | ||||||
}); | ||||||
|
||||||
// Phase 3: check if some options are required | ||||||
ArrayPrototypeForEach( | ||||||
ObjectEntries(options), | ||||||
({ 0: longOption, 1: optionConfig }) => { | ||||||
if (optionConfig.required && result.values[longOption] === undefined) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
(The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, even more paranoid and robust is
|
||||||
throw new ERR_PARSE_ARGS_REQUIRED_OPTION(longOption); | ||||||
} | ||||||
} | ||||||
); | ||||||
|
||||||
return result; | ||||||
}; | ||||||
|
||||||
|
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.
Doesn't the default depend on
strict
?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 sure what it should be🤔
But in my real code, I just check if it is a truely value.
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.
No, I think the behaviour is independent of
strict
. Options which take a value generate an error instrict
, but there is a difference between an option with a required option-argument (i.e.type='string'
) and a required option as proposed here.