Skip to content

Commit

Permalink
Merge branch 'main' into refactor-help
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Feb 29, 2024
2 parents ecf5dba + c00d5c5 commit 88e076c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 61 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Set it to `false` to disable it altogether.

##### version

Type: `string | boolean`\
Type: `string`\
Default: The package.json `"version"` property

Set a custom version output.
Expand Down Expand Up @@ -215,7 +215,7 @@ By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would

##### booleanDefault

Type: `boolean | null | undefined`\
Type: `boolean | undefined`\
Default: `false`

Value of `boolean` flags not defined in `argv`.
Expand Down
7 changes: 2 additions & 5 deletions source/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ export type Options<Flags extends AnyFlags> = {

/**
Set a custom version output. Default: The package.json `"version"` property.
Set it to `false` to disable it altogether.
*/
readonly version?: string | false;
readonly version?: string;

/**
Automatically show the help text when the `--help` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own help text.
Expand Down Expand Up @@ -291,8 +289,7 @@ export type Options<Flags extends AnyFlags> = {
//}
```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
readonly booleanDefault?: boolean | null | undefined;
readonly booleanDefault?: boolean | undefined;

// TODO: Remove this in meow 14.
/**
Expand Down
2 changes: 1 addition & 1 deletion source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const buildResult = ({pkg: packageJson, ...options}, parserOptions) => {
};

const showVersion = () => {
console.log(typeof options.version === 'string' ? options.version : packageJson.version);
console.log(options.version);
process.exit(0);
};

Expand Down
1 change: 1 addition & 0 deletions source/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const buildOptions = (helpText, options) => {
input: 'string',
description: pkg.description ?? false,
help: helpText,
version: foundPackage?.packageJson.version || 'No version found',
autoHelp: true,
autoVersion: true,
booleanDefault: false,
Expand Down
6 changes: 1 addition & 5 deletions source/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ const buildParserFlags = ({flags, booleanDefault}) => {
delete flag.shortFlag;
}

if (
booleanDefault !== undefined
&& flag.type === 'boolean'
&& !Object.hasOwn(flag, 'default')
) {
if (booleanDefault !== undefined && flag.type === 'boolean' && !Object.hasOwn(flag, 'default')) {
flag.default = flag.isMultiple ? [booleanDefault] : booleanDefault;
}

Expand Down
2 changes: 0 additions & 2 deletions test-d/build.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ expectType<Result<never>>(meow({importMeta, description: false}));
expectType<Result<never>>(meow({importMeta, help: 'foo'}));
expectType<Result<never>>(meow({importMeta, help: false}));
expectType<Result<never>>(meow({importMeta, version: 'foo'}));
expectType<Result<never>>(meow({importMeta, version: false}));
expectType<Result<never>>(meow({importMeta, autoHelp: false}));
expectType<Result<never>>(meow({importMeta, autoVersion: false}));
expectType<Result<never>>(meow({importMeta, pkg: {foo: 'bar'}}));
expectType<Result<never>>(meow({importMeta, argv: ['foo', 'bar']}));
expectType<Result<never>>(meow({importMeta, inferType: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: null}));
expectType<Result<never>>(meow({importMeta, booleanDefault: undefined}));
expectType<Result<never>>(meow({importMeta}));

Expand Down
2 changes: 0 additions & 2 deletions test-d/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ expectType<Result<never>>(meow({importMeta, description: false}));
expectType<Result<never>>(meow({importMeta, help: 'foo'}));
expectType<Result<never>>(meow({importMeta, help: false}));
expectType<Result<never>>(meow({importMeta, version: 'foo'}));
expectType<Result<never>>(meow({importMeta, version: false}));
expectType<Result<never>>(meow({importMeta, autoHelp: false}));
expectType<Result<never>>(meow({importMeta, autoVersion: false}));
expectType<Result<never>>(meow({importMeta, pkg: {foo: 'bar'}}));
expectType<Result<never>>(meow({importMeta, argv: ['foo', 'bar']}));
expectType<Result<never>>(meow({importMeta, inferType: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: null}));
expectType<Result<never>>(meow({importMeta, booleanDefault: undefined}));
expectType<Result<never>>(meow({importMeta, hardRejection: false}));

Expand Down
10 changes: 8 additions & 2 deletions test/flags/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
import test from 'ava';
import meow from '../../source/index.js';

export const _verifyFlags = importMeta => test.macro(async (t, {flags = {}, args, expected, error}) => {
export const _verifyFlags = importMeta => test.macro(async (t, {flags = {}, args, expected, error, ...meowOptions}) => {
const assertions = await t.try(async tt => {
const arguments_ = args?.split(' ') ?? [];
const meowOptions = {importMeta, argv: arguments_, flags};

meowOptions = {
...meowOptions,
importMeta,
argv: arguments_,
flags,
};

tt.log('arguments:', arguments_);

Expand Down
85 changes: 44 additions & 41 deletions test/flags/boolean-default.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
import test from 'ava';
import meow from '../../source/index.js';
import {_verifyFlags} from './_utils.js';

const importMeta = import.meta;
const verifyFlags = _verifyFlags(import.meta);

test('undefined - filter out unset boolean args', t => {
const cli = meow({
importMeta,
argv: ['--foo'],
booleanDefault: undefined,
flags: {
foo: {
type: 'boolean',
},
bar: {
type: 'boolean',
},
baz: {
type: 'boolean',
default: false,
},
test('undefined - filter out unset boolean args', verifyFlags, {
booleanDefault: undefined,
flags: {
foo: {
type: 'boolean',
},
});

t.like(cli.flags, {
bar: {
type: 'boolean',
},
baz: {
type: 'boolean',
default: false,
},
},
args: '--foo',
expected: {
foo: true,
bar: undefined,
baz: false,
});
},
});

test('boolean args are false by default', t => {
const cli = meow({
importMeta,
argv: ['--foo'],
flags: {
foo: {
type: 'boolean',
},
bar: {
type: 'boolean',
default: true,
},
baz: {
type: 'boolean',
},
test('boolean args are false by default', verifyFlags, {
flags: {
foo: {
type: 'boolean',
},
});

t.like(cli.flags, {
bar: {
type: 'boolean',
default: true,
},
baz: {
type: 'boolean',
},
},
args: '--foo',
expected: {
foo: true,
bar: true,
baz: false,
});
},
});

test('throws if default is null', verifyFlags, {
booleanDefault: null,
flags: {
foo: {
type: 'boolean',
},
},
args: '--foo',
error: 'Expected "foo" default value to be of type "boolean", got "null"',
});
8 changes: 7 additions & 1 deletion test/options/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ test('custom version', verifyVersion, {
test('version = false has no effect', verifyVersion, {
args: '--version',
execaOptions: {env: {VERSION: 'false'}},
expected: '1.0.0',
expected: 'false',
});

test('manual showVersion', verifyVersion, {
args: '--show-version',
expected: '1.0.0',
});

test('no version fallback message', verifyVersion, {
fixture: 'with-package-json/default/fixture.js',
args: '--version',
expected: 'No version found',
});

0 comments on commit 88e076c

Please sign in to comment.