diff --git a/index.js b/index.js
index fc7e24f..c4878ec 100644
--- a/index.js
+++ b/index.js
@@ -147,6 +147,17 @@ const meow = (helpText, options = {}) => {
delete parserOptions.arguments;
}
+ // Add --help and --version to known flags if autoHelp or autoVersion are set
+ if (!options.allowUnknownFlags) {
+ if (options.autoHelp) {
+ parserOptions.help = {type: 'boolean'};
+ }
+
+ if (options.autoVersion) {
+ parserOptions.version = {type: 'boolean'};
+ }
+ }
+
parserOptions = buildParserOptions(parserOptions);
parserOptions.configuration = {
diff --git a/test/allow-unknown-flags.js b/test/allow-unknown-flags.js
new file mode 100644
index 0000000..8f37229
--- /dev/null
+++ b/test/allow-unknown-flags.js
@@ -0,0 +1,63 @@
+import path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import test from 'ava';
+import indentString from 'indent-string';
+import {execa} from 'execa';
+import {readPackage} from 'read-pkg';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const fixtureAllowUnknownFlags = path.join(__dirname, 'fixtures', 'fixture-allow-unknown-flags.js');
+
+test('spawn CLI and test specifying unknown flags', async t => {
+ const error = await t.throwsAsync(
+ execa(fixtureAllowUnknownFlags, ['--foo', 'bar', '--unspecified-a', '--unspecified-b', 'input-is-allowed']),
+ {
+ message: /^Command failed with exit code 2/,
+ },
+ );
+ const {stderr} = error;
+ t.regex(stderr, /Unknown flags/);
+ t.regex(stderr, /--unspecified-a/);
+ t.regex(stderr, /--unspecified-b/);
+ t.notRegex(stderr, /input-is-allowed/);
+});
+
+test('spawn CLI and test specifying known flags', async t => {
+ const {stdout} = await execa(fixtureAllowUnknownFlags, ['--foo', 'bar']);
+ t.is(stdout, 'bar');
+});
+
+test('spawn CLI and test help as a known flag', async t => {
+ const {stdout} = await execa(fixtureAllowUnknownFlags, ['--help']);
+ t.is(stdout, indentString('\nCustom description\n\nUsage\n foo \n\n', 2));
+});
+
+test('spawn CLI and test version as a known flag', async t => {
+ const pkg = await readPackage();
+ const {stdout} = await execa(fixtureAllowUnknownFlags, ['--version']);
+ t.is(stdout, pkg.version);
+});
+
+test('spawn CLI and test help as an unknown flag', async t => {
+ const error = await t.throwsAsync(
+ execa(fixtureAllowUnknownFlags, ['--help', '--no-auto-help']),
+ {
+ message: /^Command failed with exit code 2/,
+ },
+ );
+ const {stderr} = error;
+ t.regex(stderr, /Unknown flag/);
+ t.regex(stderr, /--help/);
+});
+
+test('spawn CLI and test version as an unknown flag', async t => {
+ const error = await t.throwsAsync(
+ execa(fixtureAllowUnknownFlags, ['--version', '--no-auto-version']),
+ {
+ message: /^Command failed with exit code 2/,
+ },
+ );
+ const {stderr} = error;
+ t.regex(stderr, /Unknown flag/);
+ t.regex(stderr, /--version/);
+});
diff --git a/test/allow-unkonwn-flags.js b/test/allow-unkonwn-flags.js
deleted file mode 100644
index 5008a82..0000000
--- a/test/allow-unkonwn-flags.js
+++ /dev/null
@@ -1,26 +0,0 @@
-import path from 'node:path';
-import {fileURLToPath} from 'node:url';
-import test from 'ava';
-import {execa} from 'execa';
-
-const __dirname = path.dirname(fileURLToPath(import.meta.url));
-const fixtureAllowUnknownFlags = path.join(__dirname, 'fixtures', 'fixture-allow-unknown-flags.js');
-
-test('spawn CLI and test specifying unknown flags', async t => {
- const error = await t.throwsAsync(
- execa(fixtureAllowUnknownFlags, ['--foo', 'bar', '--unspecified-a', '--unspecified-b', 'input-is-allowed']),
- {
- message: /^Command failed with exit code 2/,
- },
- );
- const {stderr} = error;
- t.regex(stderr, /Unknown flags/);
- t.regex(stderr, /--unspecified-a/);
- t.regex(stderr, /--unspecified-b/);
- t.notRegex(stderr, /input-is-allowed/);
-});
-
-test('spawn CLI and test specifying known flags', async t => {
- const {stdout} = await execa(fixtureAllowUnknownFlags, ['--foo', 'bar']);
- t.is(stdout, 'bar');
-});
diff --git a/test/fixtures/fixture-allow-unknown-flags.js b/test/fixtures/fixture-allow-unknown-flags.js
index 3374b4f..15fc53c 100755
--- a/test/fixtures/fixture-allow-unknown-flags.js
+++ b/test/fixtures/fixture-allow-unknown-flags.js
@@ -1,4 +1,5 @@
#!/usr/bin/env node
+import process from 'node:process';
import meow from '../../index.js';
const cli = meow({
@@ -8,11 +9,20 @@ const cli = meow({
Usage
foo
`,
+ autoVersion: !process.argv.includes('--no-auto-version'),
+ autoHelp: !process.argv.includes('--no-auto-help'),
allowUnknownFlags: false,
flags: {
foo: {
type: 'string',
},
+ // For testing we need those as known flags
+ noAutoHelp: {
+ type: 'boolean',
+ },
+ noAutoVersion: {
+ type: 'boolean',
+ },
},
});