2 var path = require('path');
3 var minimist = require('minimist');
4 var objectAssign = require('object-assign');
5 var camelcaseKeys = require('camelcase-keys');
6 var decamelize = require('decamelize');
7 var mapObj = require('map-obj');
8 var trimNewlines = require('trim-newlines');
9 var redent = require('redent');
10 var readPkgUp = require('read-pkg-up');
11 var loudRejection = require('loud-rejection');
12 var normalizePackageData = require('normalize-package-data');
14 // get the uncached parent
15 delete require.cache[__filename];
16 var parentDir = path.dirname(module.parent.filename);
18 module.exports = function (opts, minimistOpts) {
21 if (Array.isArray(opts) || typeof opts === 'string') {
30 argv: process.argv.slice(2)
33 minimistOpts = objectAssign({}, minimistOpts);
35 minimistOpts.default = mapObj(minimistOpts.default || {}, function (key, value) {
36 return [decamelize(key, '-'), value];
39 if (Array.isArray(opts.help)) {
40 opts.help = opts.help.join('\n');
43 var pkg = typeof opts.pkg === 'string' ? require(path.join(parentDir, opts.pkg)) : opts.pkg;
44 var argv = minimist(opts.argv, minimistOpts);
45 var help = redent(trimNewlines(opts.help || ''), 2);
47 normalizePackageData(pkg);
49 process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;
51 var description = opts.description;
52 if (!description && description !== false) {
53 description = pkg.description;
56 help = (description ? '\n ' + description + '\n' : '') + (help ? '\n' + help : '\n');
58 var showHelp = function (code) {
60 process.exit(code || 0);
63 if (argv.version && opts.version !== false) {
64 console.log(typeof opts.version === 'string' ? opts.version : pkg.version);
68 if (argv.help && opts.help !== false) {
77 flags: camelcaseKeys(argv),