Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/meow
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v10.1.3
Choose a base ref
...
head repository: sindresorhus/meow
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v10.1.4
Choose a head ref
  • 2 commits
  • 5 files changed
  • 2 contributors

Commits on Sep 27, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    25a0903 View commit details
  2. 10.1.4

    sindresorhus committed Sep 27, 2022
    Copy the full SHA
    0b07524 View commit details
Showing with 85 additions and 27 deletions.
  1. +11 −0 index.js
  2. +1 −1 package.json
  3. +63 −0 test/allow-unknown-flags.js
  4. +0 −26 test/allow-unkonwn-flags.js
  5. +10 −0 test/fixtures/fixture-allow-unknown-flags.js
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = {
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meow",
"version": "10.1.3",
"version": "10.1.4",
"description": "CLI app helper",
"license": "MIT",
"repository": "sindresorhus/meow",
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({
@@ -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',
},
},
});