From 61562bdae39671fab19787a1e5635bb05221bec0 Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Tue, 23 Apr 2013 11:28:58 -0700 Subject: [PATCH] Allow optional non-Optimist argv. --- index.js | 5 +++-- test/test.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0fd8a11..110fb81 100755 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ #! /usr/bin/env node -var argv = require('optimist').argv var cc = require('./lib/utils') var join = require('path').join var deepExtend = require('deep-extend') @@ -9,9 +8,11 @@ var home = win ? process.env.USERPROFILE : process.env.HOME -module.exports = function (name, defaults) { +module.exports = function (name, defaults, argv) { if(!name) throw new Error('nameless configuration fail') + if(!argv) + argv = require('optimist').argv defaults = ( 'string' === typeof defaults ? cc.json(defaults) : defaults diff --git a/test/test.js b/test/test.js index 547a7bb..9d9fdfe 100644 --- a/test/test.js +++ b/test/test.js @@ -12,3 +12,20 @@ console.log(config) assert.equal(config.option, true) assert.equal(config.envOption, 42) + +var customArgv = require('../')(n, { + option: true +}, { // nopt-like argv + option: false, + envOption: 24, + argv: { + remain: [], + cooked: ['--no-option', '--envOption', '24'], + original: ['--no-option', '--envOption=24'] + } +}) + +console.log(customArgv) + +assert.equal(customArgv.option, false) +assert.equal(customArgv.envOption, 24)