Skip to content
Permalink
Browse files

Rewrite flags input (#63)

  • Loading branch information...
vadimdemedes authored and sindresorhus committed Sep 23, 2017
1 parent b561b55 commit 43401c39ee442db308288f50d6f213cd0e6e602c
Showing with 61 additions and 45 deletions.
  1. +5 −8 fixture.js
  2. +16 −16 index.js
  3. +1 −0 package.json
  4. +5 −11 readme.md
  5. +34 −10 test.js
@@ -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'}
}
});

@@ -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');
@@ -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({
@@ -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);
@@ -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",
@@ -45,8 +45,11 @@ const cli = meow(`
$ foo unicorns --rainbow
🌈 unicorns 🌈
`, {
alias: {
r: 'rainbow'
flags: {
rainbow: {
type: 'boolean',
alias: 'r'
}
}
});
/*
@@ -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

44 test.js
@@ -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');
@@ -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.
You can’t perform that action at this time.