From 219a27dde51bf4c04af2d905f71d1cec021ee98e Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:20:48 -0700 Subject: fix(ext/node): Support returning tokens and option defaults in `node:util.parseArgs` (#23192) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #23179. Fixes #22454. Enables passing `{tokens: true}` to `parseArgs` and setting default values for options. With this PR, the observable framework works with deno out of the box (no unstable flags needed). The existing code was basically copied straight from node, so this PR mostly just updates that (out of date) vendored code. Also fixes some issues with error exports (before this PR, in certain error cases we were attempting to construct error classes that weren't actually in scope). The last change (in the second commit) adds a small hack so that we actually exercise the `test-parse-args.js` node_compat test, previously it was reported as passing though it should have failed. That test now passes. --------- Co-authored-by: Bartek IwaƄczuk --- ext/node/polyfills/internal/util/parse_args/utils.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'ext/node/polyfills/internal/util/parse_args/utils.js') diff --git a/ext/node/polyfills/internal/util/parse_args/utils.js b/ext/node/polyfills/internal/util/parse_args/utils.js index a51a46098..1ceed0b9e 100644 --- a/ext/node/polyfills/internal/util/parse_args/utils.js +++ b/ext/node/polyfills/internal/util/parse_args/utils.js @@ -173,6 +173,18 @@ function findLongOptionForShort(shortOption, options) { return longOptionEntry?.[0] ?? shortOption; } +/** + * Check if the given option includes a default value + * and that option has not been set by the input args. + * @param {string} longOption - long option name e.g. 'foo' + * @param {object} optionConfig - the option configuration properties + * @param {object} values - option values returned in `values` by parseArgs + */ +function useDefaultValueOption(longOption, optionConfig, values) { + return objectGetOwn(optionConfig, "default") !== undefined && + values[longOption] === undefined; +} + export { findLongOptionForShort, isLoneLongOption, @@ -184,4 +196,5 @@ export { isShortOptionGroup, objectGetOwn, optionsGetOwn, + useDefaultValueOption, }; -- cgit v1.2.3