Skip to content

Commit

Permalink
Add support for number flag type
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 5, 2018
1 parent 646f30b commit fe4a2fe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -97,7 +97,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 @@ -198,3 +198,71 @@ test('grouped short-flags work', t => {
t.true(flags.c);
t.true(flags.l);
});

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 fe4a2fe

Please sign in to comment.