Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Close #19 PR: add type inference option. Fixes #17
  • Loading branch information
SamVerschueren authored and sindresorhus committed Dec 16, 2015
1 parent 9812414 commit 1662881
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
13 changes: 12 additions & 1 deletion index.js
Expand Up @@ -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');
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"ava": "*",
"execa": "^0.1.1",
"indent-string": "^2.1.0",
"xo": "*"
}
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Expand Up @@ -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`
Expand Down
50 changes: 21 additions & 29 deletions 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'],
Expand All @@ -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 => {
Expand All @@ -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.