Skip to content

Commit

Permalink
Rewrite flags input (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes authored and sindresorhus committed Sep 23, 2017
1 parent b561b55 commit 43401c3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
13 changes: 5 additions & 8 deletions fixture.js
Expand Up @@ -7,14 +7,11 @@ const cli = meow({
help: `
Usage
foo <input>
`
}, {
alias: {
u: 'unicorn'
},
default: {
meow: 'dog',
camelCaseOption: 'foo'
`,
flags: {
unicorn: {alias: 'u'},
meow: {default: 'dog'},
camelCaseOption: {default: 'foo'}
}
});

Expand Down
32 changes: 16 additions & 16 deletions index.js
@@ -1,5 +1,6 @@
'use strict';
const path = require('path');
const buildMinimistOptions = require('minimist-options');
const minimist = require('minimist');
const camelcaseKeys = require('camelcase-keys');
const decamelizeKeys = require('decamelize-keys');
Expand All @@ -13,13 +14,12 @@ const normalizePackageData = require('normalize-package-data');
delete require.cache[__filename];
const parentDir = path.dirname(module.parent.filename);

module.exports = (opts, minimistOpts) => {
module.exports = (helpMessage, opts) => {
loudRejection();

if (Array.isArray(opts) || typeof opts === 'string') {
opts = {
help: opts
};
if (typeof helpMessage === 'object' && !Array.isArray(helpMessage)) {
opts = helpMessage;
helpMessage = '';
}

opts = Object.assign({
Expand All @@ -28,23 +28,23 @@ module.exports = (opts, minimistOpts) => {
normalize: false
}).pkg,
argv: process.argv.slice(2),
inferType: false
inferType: false,
input: 'string',
help: helpMessage
}, opts);

minimistOpts = Object.assign({
string: ['_']
}, minimistOpts);
let minimistOpts = Object.assign({
arguments: opts.input
}, opts.flags);

minimistOpts.default = decamelizeKeys(minimistOpts.default || {}, '-');
minimistOpts = decamelizeKeys(minimistOpts, '-', {exclude: ['stopEarly', '--']});

const 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 (opts.inferType) {
delete minimistOpts.arguments;
}

minimistOpts = buildMinimistOptions(minimistOpts);

const pkg = opts.pkg;
const argv = minimist(opts.argv, minimistOpts);
let help = redent(trimNewlines((opts.help || '').replace(/\t+\n*$/, '')), 2);
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"decamelize-keys": "^1.0.0",
"loud-rejection": "^1.0.0",
"minimist": "^1.1.3",
"minimist-options": "^3.0.1",
"normalize-package-data": "^2.3.4",
"read-pkg-up": "^2.0.0",
"redent": "^2.0.0",
Expand Down
16 changes: 5 additions & 11 deletions readme.md
Expand Up @@ -45,8 +45,11 @@ const cli = meow(`
$ foo unicorns --rainbow
🌈 unicorns 🌈
`, {
alias: {
r: 'rainbow'
flags: {
rainbow: {
type: 'boolean',
alias: 'r'
}
}
});
/*
Expand Down Expand Up @@ -134,15 +137,6 @@ 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`<br>
Default: `{}`

Minimist [options](https://github.com/substack/minimist#var-argv--parseargsargs-opts).

Keys passed to the minimist `default` option are [decamelized](https://github.com/sindresorhus/decamelize), so you can for example pass in `fooBar: 'baz'` and have it be the default for the `--foo-bar` flag.


## Promises

Expand Down
44 changes: 34 additions & 10 deletions test.js
Expand Up @@ -10,11 +10,12 @@ test('return object', t => {
help: `
Usage
foo <input>
`
}, {
alias: {u: 'unicorn'},
default: {meow: 'dog'},
'--': true
`,
flags: {
unicorn: {alias: 'u'},
meow: {default: 'dog'},
'--': true
}
});

t.is(cli.input[0], 'foo');
Expand Down Expand Up @@ -72,17 +73,40 @@ test('single character flag casing should be preserved', t => {

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

test('accept help and options', t => {
t.deepEqual(m('help', {
argv: ['-f'],
flags: {
foo: {
type: 'boolean',
alias: 'f'
}
}
}).flags, {
foo: true,
f: true
});
});

0 comments on commit 43401c3

Please sign in to comment.