Repository: babel/babel (TypeScript)

Issue:
[Bug]: error "unknown option" raised when using @babel/node as CLI
### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

@babel/register or @babel/node

### Input code

Since the latest version of `@babel/node` I can no longer use custom args in CLI. I guess it's related to the commander update.

I've created a repo to reproduce the bug: https://github.com/j0k3r/babel-commander-bug

The [first commit](https://github.com/j0k3r/babel-commander-bug/commit/3b195a48ab848b0276bf59a733ee313dc6a5637b) is using `@babel/node @ 7.23.9` and the output of the `scripts.js` is:

```bash
$ yarn babel-node script.js --dry-run
yarn run v1.22.21
warning package.json: No license field
$ /private/tmp/babel/node_modules/.bin/babel-node script.js --dry-run
{ developer: 'bob', 'dry-run': true }
✨  Done in 1.06s.
```

The [second commit](https://github.com/j0k3r/babel-commander-bug/commit/c2233e3bbfc363892b4b83e50b39bb1ca719fa05) is using the latest version (ie: 7.24.6) and I got an error:

```bash
$ yarn babel-node script.js --dry-run
yarn run v1.22.21
warning package.json: No license field
$ /private/tmp/babel/node_modules/.bin/babel-node script.js --dry-run
error: unknown option '--dry-run'
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
```

### Configuration file name

package.json

### Configuration

_No response_

### Current and expected behavior

Both version should be fine with custom args in CLI.

### Environment

- Babel version: 7.24.6
- node version is unrelated I guess

### Possible solution

Maybe I do it wrong when using `babel-node` as CLI?

### Additional context

_No response_


Hints:
Hey @j0k3r! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant [Slack community](https://babeljs.slack.com) that typically always has someone willing to help. You can sign-up [here](https://slack.babeljs.io/) for an invite.

Patch:
diff --git a/packages/babel-node/src/_babel-node.ts b/packages/babel-node/src/_babel-node.ts
--- a/packages/babel-node/src/_babel-node.ts
+++ b/packages/babel-node/src/_babel-node.ts
@@ -71,6 +71,8 @@ program.option(
 program.option("-w, --plugins [string]", "", collect);
 program.option("-b, --presets [string]", "", collect);
 
+program.allowUnknownOption(true);
+
 program.version(PACKAGE_JSON.version);
 program.usage("[options] [ -e script | script.js ] [arguments]");
 program.parse(process.argv);


Test patch:
diff --git a/packages/babel-node/test/fixtures/cli/extra-flags/in-files/bar.js b/packages/babel-node/test/fixtures/cli/extra-flags/in-files/bar.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-node/test/fixtures/cli/extra-flags/in-files/bar.js
@@ -0,0 +1 @@
+console.log(process.argv.slice(2));
diff --git a/packages/babel-node/test/fixtures/cli/extra-flags/options.json b/packages/babel-node/test/fixtures/cli/extra-flags/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-node/test/fixtures/cli/extra-flags/options.json
@@ -0,0 +1,3 @@
+{
+  "args": ["bar", "--extra-option"]
+}
diff --git a/packages/babel-node/test/fixtures/cli/extra-flags/stdout.txt b/packages/babel-node/test/fixtures/cli/extra-flags/stdout.txt
new file mode 100644
--- /dev/null
+++ b/packages/babel-node/test/fixtures/cli/extra-flags/stdout.txt
@@ -0,0 +1 @@
+[ '--extra-option' ]