Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for custom config for help #217

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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'};
}
}
12 changes: 12 additions & 0 deletions test/allow-unknown-flags.js
Original file line number Diff line number Diff line change
@@ -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(
@@ -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);