Skip to content
Permalink
Browse files

Close #19 PR: add type inference option. Fixes #17

  • Loading branch information...
SamVerschueren authored and sindresorhus committed Dec 16, 2015
1 parent 9812414 commit 1662881674c6b243226314e35d46115ea66329ff
Showing with 43 additions and 30 deletions.
  1. +12 −1 index.js
  2. +1 −0 package.json
  3. +9 −0 readme.md
  4. +21 −29 test.js
@@ -25,9 +25,20 @@ module.exports = function (opts, minimistOpts) {
cwd: parentDir,
normalize: false
}).pkg,
argv: process.argv.slice(2)
argv: process.argv.slice(2),
inferType: false
}, opts);

minimistOpts = objectAssign({string: ['_']}, minimistOpts);

var index = minimistOpts.string.indexOf('_');

if (opts.inferType === false && index === -1) {
minimistOpts.string.push('_');
} else if (opts.inferType === true && index !== -1) {
minimistOpts.string.splice(index, 1);
}

if (Array.isArray(opts.help)) {
opts.help = opts.help.join('\n');
}
@@ -49,6 +49,7 @@
},
"devDependencies": {
"ava": "*",
"execa": "^0.1.1",
"indent-string": "^2.1.0",
"xo": "*"
}
@@ -126,6 +126,15 @@ Default: `process.argv.slice(2)`

Custom arguments object.

##### inferType

Type: `boolean`
Default: `false`

Infer the argument type.

By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would infer it as a number.

#### minimistOptions

Type: `object`
50 test.js
@@ -1,8 +1,11 @@
import childProcess from 'child_process';
import test from 'ava';
import indentString from 'indent-string';
import execa from 'execa';
import pkg from './package.json';
import fn from './';

global.Promise = Promise;

test('return object', t => {
const cli = fn({
argv: ['foo', '--foo-bar', '-u', 'cat'],
@@ -21,46 +24,29 @@ test('return object', t => {
t.is(cli.flags.unicorn, 'cat');
t.is(cli.pkg.name, 'meow');
t.is(cli.help, indentString('\nCLI app helper\n\nUsage\n foo <input>', ' '));
t.end();
});

test('support help shortcut', t => {
const cli = fn(['unicorn', 'cat']);
t.is(cli.help, indentString('\nCLI app helper\n\nunicorn\ncat', ' '));
t.end();
});

test('spawn cli and show version', t => {
t.plan(2);
test('spawn cli and show version', async t => {
const {stdout} = await execa('./fixture.js', ['--version']);

childProcess.execFile('./fixture.js', ['--version'], {
cwd: __dirname
}, (err, stdout) => {
t.ifError(err);
t.is(stdout.trim(), require('./package.json').version);
});
t.is(stdout, pkg.version);
});

test('spawn cli and show help screen', t => {
t.plan(2);
test('spawn cli and show help screen', async t => {
const {stdout} = await execa('./fixture.js', ['--help']);

childProcess.execFile('./fixture.js', ['--help'], {
cwd: __dirname
}, (err, stdout) => {
t.ifError(err);
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\n', ' '));
});
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>', ' '));
});

test('spawn cli and test input', t => {
t.plan(2);
test('spawn cli and test input', async t => {
const {stdout} = await execa('./fixture.js', ['-u', 'cat']);

childProcess.execFile('./fixture.js', ['-u', 'cat'], {
cwd: __dirname
}, (err, stdout) => {
t.ifError(err);
t.is(stdout, 'u\nunicorn\nmeow\n');
});
t.is(stdout, 'u\nunicorn\nmeow');
});

test.serial('pkg.bin as a string should work', t => {
@@ -72,10 +58,16 @@ test.serial('pkg.bin as a string should work', t => {
});

t.is(process.title, 'browser-sync');
t.end();
});

test('single character flag casing should be preserved', t => {
t.ok(fn({argv: ['-F']}).flags.F);
t.end();
});

test('type inference', t => {
t.is(fn({argv: ['5']}).input[0], '5');
t.is(fn({argv: ['5']}, {string: ['_']}).input[0], '5');
t.is(fn({argv: ['5'], inferType: true}).input[0], 5);
t.is(fn({argv: ['5'], inferType: true}, {string: ['foo']}).input[0], 5);
t.is(fn({argv: ['5'], inferType: true}, {string: ['_', 'foo']}).input[0], 5);
});

0 comments on commit 1662881

Please sign in to comment.
You can’t perform that action at this time.