Skip to content

Commit

Permalink
Fix autoHelp and autoVersion with allowUnknownFlags set to false (
Browse files Browse the repository at this point in the history
  • Loading branch information
bartaz committed Sep 27, 2022
1 parent 85dc1e9 commit 25a0903
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 26 deletions.
11 changes: 11 additions & 0 deletions index.js
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
@@ -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
@@ -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

0 comments on commit 25a0903

Please sign in to comment.