Skip to content

Commit 5ef9478

Browse files
authoredNov 17, 2019
Add support for number flag type (#103)
1 parent 8e5248e commit 5ef9478

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare namespace meow {
88
99
The key is the flag name and the value is an object with any of:
1010
11-
- `type`: Type of value. (Possible values: `string` `boolean`)
11+
- `type`: Type of value. (Possible values: `string` `boolean` `number`)
1212
- `alias`: Usually used to define a short flag alias.
1313
- `default`: Default value when the flag is not specified.
1414

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Define argument flags.
9898

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

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

‎test.js

+68
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,71 @@ test('disable autoVersion/autoHelp if `cli.input.length > 0`', t => {
240240
t.is(meow({argv: ['bar', '--help']}).input[0], 'bar');
241241
t.is(meow({argv: ['bar', '--version', '--help']}).input[0], 'bar');
242242
});
243+
244+
test('supports `number` flag type', t => {
245+
const cli = meow({
246+
argv: ['--foo=1.3'],
247+
flags: {
248+
foo: {
249+
type: 'number'
250+
}
251+
}
252+
}).flags.foo;
253+
254+
t.is(cli, 1.3);
255+
});
256+
257+
test('supports `number` flag type - flag but no value', t => {
258+
const cli = meow({
259+
argv: ['--foo'],
260+
flags: {
261+
foo: {
262+
type: 'number'
263+
}
264+
}
265+
}).flags.foo;
266+
267+
t.is(cli, undefined);
268+
});
269+
270+
test('supports `number` flag type - flag but no value but default', t => {
271+
const cli = meow({
272+
argv: ['--foo'],
273+
flags: {
274+
foo: {
275+
type: 'number',
276+
default: 2
277+
}
278+
}
279+
}).flags.foo;
280+
281+
t.is(cli, 2);
282+
});
283+
284+
test('supports `number` flag type - no flag but default', t => {
285+
const cli = meow({
286+
argv: [],
287+
flags: {
288+
foo: {
289+
type: 'number',
290+
default: 2
291+
}
292+
}
293+
}).flags.foo;
294+
295+
t.is(cli, 2);
296+
});
297+
298+
test('supports `number` flag type - throws on incorrect default value', t => {
299+
t.throws(() => {
300+
meow({
301+
argv: [],
302+
flags: {
303+
foo: {
304+
type: 'number',
305+
default: 'x'
306+
}
307+
}
308+
});
309+
});
310+
});

0 commit comments

Comments
 (0)
Please sign in to comment.