Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix autoHelp and autoVersion with allowUnknownFlags set to false #215

Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
63 changes: 63 additions & 0 deletions test/allow-unknown-flags.js
Original file line number Diff line number Diff line change
@@ -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 <input>\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/);
});
26 changes: 0 additions & 26 deletions test/allow-unkonwn-flags.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/fixtures/fixture-allow-unknown-flags.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
import process from 'node:process';
import meow from '../../index.js';

const cli = meow({
Expand All @@ -8,11 +9,20 @@ const cli = meow({
Usage
foo <input>
`,
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',
},
},
});

Expand Down