Skip to content

Commit

Permalink
Add support for number flag type (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 17, 2019
1 parent 8e5248e commit 5ef9478
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
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'
}
}
});
});
});

0 comments on commit 5ef9478

Please sign in to comment.