Skip to content

Commit 25a0903

Browse files
authoredSep 27, 2022
Fix autoHelp and autoVersion with allowUnknownFlags set to false (#215)
1 parent 85dc1e9 commit 25a0903

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed
 

‎index.js

+11
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ const meow = (helpText, options = {}) => {
147147
delete parserOptions.arguments;
148148
}
149149

150+
// Add --help and --version to known flags if autoHelp or autoVersion are set
151+
if (!options.allowUnknownFlags) {
152+
if (options.autoHelp) {
153+
parserOptions.help = {type: 'boolean'};
154+
}
155+
156+
if (options.autoVersion) {
157+
parserOptions.version = {type: 'boolean'};
158+
}
159+
}
160+
150161
parserOptions = buildParserOptions(parserOptions);
151162

152163
parserOptions.configuration = {

‎test/allow-unknown-flags.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
3+
import test from 'ava';
4+
import indentString from 'indent-string';
5+
import {execa} from 'execa';
6+
import {readPackage} from 'read-pkg';
7+
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
const fixtureAllowUnknownFlags = path.join(__dirname, 'fixtures', 'fixture-allow-unknown-flags.js');
10+
11+
test('spawn CLI and test specifying unknown flags', async t => {
12+
const error = await t.throwsAsync(
13+
execa(fixtureAllowUnknownFlags, ['--foo', 'bar', '--unspecified-a', '--unspecified-b', 'input-is-allowed']),
14+
{
15+
message: /^Command failed with exit code 2/,
16+
},
17+
);
18+
const {stderr} = error;
19+
t.regex(stderr, /Unknown flags/);
20+
t.regex(stderr, /--unspecified-a/);
21+
t.regex(stderr, /--unspecified-b/);
22+
t.notRegex(stderr, /input-is-allowed/);
23+
});
24+
25+
test('spawn CLI and test specifying known flags', async t => {
26+
const {stdout} = await execa(fixtureAllowUnknownFlags, ['--foo', 'bar']);
27+
t.is(stdout, 'bar');
28+
});
29+
30+
test('spawn CLI and test help as a known flag', async t => {
31+
const {stdout} = await execa(fixtureAllowUnknownFlags, ['--help']);
32+
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\n\n', 2));
33+
});
34+
35+
test('spawn CLI and test version as a known flag', async t => {
36+
const pkg = await readPackage();
37+
const {stdout} = await execa(fixtureAllowUnknownFlags, ['--version']);
38+
t.is(stdout, pkg.version);
39+
});
40+
41+
test('spawn CLI and test help as an unknown flag', async t => {
42+
const error = await t.throwsAsync(
43+
execa(fixtureAllowUnknownFlags, ['--help', '--no-auto-help']),
44+
{
45+
message: /^Command failed with exit code 2/,
46+
},
47+
);
48+
const {stderr} = error;
49+
t.regex(stderr, /Unknown flag/);
50+
t.regex(stderr, /--help/);
51+
});
52+
53+
test('spawn CLI and test version as an unknown flag', async t => {
54+
const error = await t.throwsAsync(
55+
execa(fixtureAllowUnknownFlags, ['--version', '--no-auto-version']),
56+
{
57+
message: /^Command failed with exit code 2/,
58+
},
59+
);
60+
const {stderr} = error;
61+
t.regex(stderr, /Unknown flag/);
62+
t.regex(stderr, /--version/);
63+
});

‎test/allow-unkonwn-flags.js

-26
This file was deleted.

‎test/fixtures/fixture-allow-unknown-flags.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
import process from 'node:process';
23
import meow from '../../index.js';
34

45
const cli = meow({
@@ -8,11 +9,20 @@ const cli = meow({
89
Usage
910
foo <input>
1011
`,
12+
autoVersion: !process.argv.includes('--no-auto-version'),
13+
autoHelp: !process.argv.includes('--no-auto-help'),
1114
allowUnknownFlags: false,
1215
flags: {
1316
foo: {
1417
type: 'string',
1518
},
19+
// For testing we need those as known flags
20+
noAutoHelp: {
21+
type: 'boolean',
22+
},
23+
noAutoVersion: {
24+
type: 'boolean',
25+
},
1626
},
1727
});
1828

0 commit comments

Comments
 (0)
Please sign in to comment.