Skip to content
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

node compat: parseArgs doesn't support default option #22454

Closed
sigmaSd opened this issue Feb 17, 2024 · 4 comments · Fixed by #23192
Closed

node compat: parseArgs doesn't support default option #22454

sigmaSd opened this issue Feb 17, 2024 · 4 comments · Fixed by #23192

Comments

@sigmaSd
Copy link
Contributor

sigmaSd commented Feb 17, 2024

Version: Deno 1.40.5

import { parseArgs } from "node:util";

const parsedArgs = parseArgs({
  options: { hello: { type: "string" } },
  allowPositionals: true,
});

console.log(parsedArgs);

node a.js --args hello a

{ values: [Object: null prototype] { hello: 'a' }, positionals: [] }

deno run a.js --args hello a

error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'execArgv')
const parsedArgs = parseArgs({
                   ^
    at getMainArgs (ext:deno_node/internal/util/parse_args/parse_args.js:53:28)
    at parseArgs (ext:deno_node/internal/util/parse_args/parse_args.js:162:48)
    at file:///home/mrcool/dev/lab/c/a.js:3:20

the above is already tracked by #22363

But the behavior is still not the same

import { parseArgs } from "node:util";

const parsedArgs = parseArgs({
  args: [],
  options: {
    handler: {
      type: "string",
      default: "hello",
    },
  },
});

console.log(parsedArgs.values.handler);

Running with not arguments

deno -> undefined
nodejs -> hello

@shadowspawn
Copy link

Fix underway: #22363 #22405

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Feb 18, 2024

I see , but there behaviour is still missing some parts:

import { parseArgs } from "node:util";

const parsedArgs = parseArgs({
  args: [],
  options: {
    handler: {
      type: "string",
      default: "hello",
    },
  },
});

console.log(parsedArgs.values.handler);

Running with no arguments

deno -> undefined
nodejs -> hello

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Feb 18, 2024

I guess its just missing support for default

@sigmaSd sigmaSd changed the title node compat: parseArgs behaves differently then node node compat: parseArgs doesn't support default option Feb 18, 2024
@shadowspawn
Copy link

The support for defaults was added in: nodejs/node#44631

satyarohith pushed a commit that referenced this issue Apr 11, 2024
…util.parseArgs` (#23192)

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 <biwanczuk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants