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

Add support for number flag type #103

Merged
merged 2 commits into from Nov 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -8,7 +8,7 @@ declare namespace meow {

The key is the flag name and the value is an object with any of:

- `type`: Type of value. (Possible values: `string` `boolean`)
- `type`: Type of value. (Possible values: `string` `boolean` `number`)
- `alias`: Usually used to define a short flag alias.
- `default`: Default value when the flag is not specified.

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -98,7 +98,7 @@ Define argument flags.

The key is the flag name and the value is an object with any of:

- `type`: Type of value. (Possible values: `string` `boolean`)
- `type`: Type of value. (Possible values: `string` `boolean` `number`)
- `alias`: Usually used to define a short flag alias.
- `default`: Default value when the flag is not specified.

Expand Down
68 changes: 68 additions & 0 deletions test.js
Expand Up @@ -240,3 +240,71 @@ test('disable autoVersion/autoHelp if `cli.input.length > 0`', t => {
t.is(meow({argv: ['bar', '--help']}).input[0], 'bar');
t.is(meow({argv: ['bar', '--version', '--help']}).input[0], 'bar');
});

test('supports `number` flag type', t => {
const cli = meow({
argv: ['--foo=1.3'],
flags: {
foo: {
type: 'number'
}
}
}).flags.foo;

t.is(cli, 1.3);
});

test('supports `number` flag type - flag but no value', t => {
const cli = meow({
argv: ['--foo'],
flags: {
foo: {
type: 'number'
}
}
}).flags.foo;

t.is(cli, undefined);
});

test('supports `number` flag type - flag but no value but default', t => {
const cli = meow({
argv: ['--foo'],
flags: {
foo: {
type: 'number',
default: 2
}
}
}).flags.foo;

t.is(cli, 2);
});

test('supports `number` flag type - no flag but default', t => {
const cli = meow({
argv: [],
flags: {
foo: {
type: 'number',
default: 2
}
}
}).flags.foo;

t.is(cli, 2);
});

test('supports `number` flag type - throws on incorrect default value', t => {
t.throws(() => {
meow({
argv: [],
flags: {
foo: {
type: 'number',
default: 'x'
}
}
});
});
});