Skip to content

Commit d0dc0d7

Browse files
authored
Fix for custom config for help (#217)
1 parent 0b07524 commit d0dc0d7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ const meow = (helpText, options = {}) => {
149149

150150
// Add --help and --version to known flags if autoHelp or autoVersion are set
151151
if (!options.allowUnknownFlags) {
152-
if (options.autoHelp) {
152+
if (options.autoHelp && !parserOptions.help) {
153153
parserOptions.help = {type: 'boolean'};
154154
}
155155

156-
if (options.autoVersion) {
156+
if (options.autoVersion && !parserOptions.version) {
157157
parserOptions.version = {type: 'boolean'};
158158
}
159159
}

test/allow-unknown-flags.js

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {readPackage} from 'read-pkg';
77

88
const __dirname = path.dirname(fileURLToPath(import.meta.url));
99
const fixtureAllowUnknownFlags = path.join(__dirname, 'fixtures', 'fixture-allow-unknown-flags.js');
10+
const fixtureAllowUnknownFlagsWithHelp = path.join(__dirname, 'fixtures', 'fixture-allow-unknown-flags-with-help.js');
1011

1112
test('spawn CLI and test specifying unknown flags', async t => {
1213
const error = await t.throwsAsync(
@@ -61,3 +62,14 @@ test('spawn CLI and test version as an unknown flag', async t => {
6162
t.regex(stderr, /Unknown flag/);
6263
t.regex(stderr, /--version/);
6364
});
65+
66+
test('spawn CLI and test help with custom config', async t => {
67+
const {stdout} = await execa(fixtureAllowUnknownFlagsWithHelp, ['-h']);
68+
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\n\n', 2));
69+
});
70+
71+
test('spawn CLI and test version with custom config', async t => {
72+
const pkg = await readPackage();
73+
const {stdout} = await execa(fixtureAllowUnknownFlagsWithHelp, ['-v']);
74+
t.is(stdout, pkg.version);
75+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env node
2+
import meow from '../../index.js';
3+
4+
const cli = meow({
5+
importMeta: import.meta,
6+
description: 'Custom description',
7+
help: `
8+
Usage
9+
foo <input>
10+
`,
11+
allowUnknownFlags: false,
12+
flags: {
13+
help: {
14+
alias: 'h',
15+
type: 'boolean',
16+
},
17+
version: {
18+
alias: 'v',
19+
type: 'boolean',
20+
},
21+
},
22+
});
23+
24+
console.log(cli.flags.help);

0 commit comments

Comments
 (0)