Skip to content

Commit

Permalink
Fix for custom config for help (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartaz authored Oct 3, 2022
1 parent 0b07524 commit d0dc0d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ const meow = (helpText, options = {}) => {

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

if (options.autoVersion) {
if (options.autoVersion && !parserOptions.version) {
parserOptions.version = {type: 'boolean'};
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/allow-unknown-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {readPackage} from 'read-pkg';

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

test('spawn CLI and test specifying unknown flags', async t => {
const error = await t.throwsAsync(
Expand Down Expand Up @@ -61,3 +62,14 @@ test('spawn CLI and test version as an unknown flag', async t => {
t.regex(stderr, /Unknown flag/);
t.regex(stderr, /--version/);
});

test('spawn CLI and test help with custom config', async t => {
const {stdout} = await execa(fixtureAllowUnknownFlagsWithHelp, ['-h']);
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\n\n', 2));
});

test('spawn CLI and test version with custom config', async t => {
const pkg = await readPackage();
const {stdout} = await execa(fixtureAllowUnknownFlagsWithHelp, ['-v']);
t.is(stdout, pkg.version);
});
24 changes: 24 additions & 0 deletions test/fixtures/fixture-allow-unknown-flags-with-help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node
import meow from '../../index.js';

const cli = meow({
importMeta: import.meta,
description: 'Custom description',
help: `
Usage
foo <input>
`,
allowUnknownFlags: false,
flags: {
help: {
alias: 'h',
type: 'boolean',
},
version: {
alias: 'v',
type: 'boolean',
},
},
});

console.log(cli.flags.help);

0 comments on commit d0dc0d7

Please # to comment.