diff --git a/fixture.js b/fixture.js index bde9c75..7ea1c82 100755 --- a/fixture.js +++ b/fixture.js @@ -7,14 +7,11 @@ const cli = meow({ help: ` Usage foo - ` -}, { - alias: { - u: 'unicorn' - }, - default: { - meow: 'dog', - camelCaseOption: 'foo' + `, + flags: { + unicorn: {alias: 'u'}, + meow: {default: 'dog'}, + camelCaseOption: {default: 'foo'} } }); diff --git a/index.js b/index.js index 9bd5908..d7b4f88 100644 --- a/index.js +++ b/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'); @@ -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); diff --git a/package.json b/package.json index f5b3d8a..40a0e8f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index 3999bdc..73d5397 100644 --- a/readme.md +++ b/readme.md @@ -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`
-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 diff --git a/test.js b/test.js index 8371504..d146226 100644 --- a/test.js +++ b/test.js @@ -10,11 +10,12 @@ test('return object', t => { help: ` Usage foo - ` - }, { - 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 + }); });