From 3e26bb27569e90370353ce05ecc5c00afaa4c598 Mon Sep 17 00:00:00 2001 From: Nicolas Deveaud Date: Wed, 6 Jan 2016 23:47:14 +0100 Subject: [PATCH] Add posibility to pass a yargs instance to argv() method --- README.md | 17 +++++++++++++++++ lib/nconf/stores/argv.js | 12 +++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2adf600..bf48f5c 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,23 @@ If the return value is falsey, the entry will be dropped from the store, otherwi }); ``` +It's also possible to pass a configured yargs instance + +``` js + nconf.argv(require('yargs') + .version('1.2.3') + .usage('My usage definition') + .strict() + .options({ + "x": { + alias: 'example', + describe: 'Example description for usage generation', + demand: true, + default: 'some-value' + } + })); +``` + ### Env Responsible for loading the values parsed from `process.env` into the configuration hierarchy. By default, the env variables values are loaded into the configuration as strings. diff --git a/lib/nconf/stores/argv.js b/lib/nconf/stores/argv.js index 8fffce8..c418a2b 100644 --- a/lib/nconf/stores/argv.js +++ b/lib/nconf/stores/argv.js @@ -48,9 +48,11 @@ Argv.prototype.loadArgv = function () { var self = this, yargs, argv; - yargs = typeof this.options === 'object' - ? require('yargs')(process.argv.slice(2)).options(this.options) - : require('yargs')(process.argv.slice(2)); + yargs = isYargs(this.options) ? + this.options : + typeof this.options === 'object' ? + require('yargs')(process.argv.slice(2)).options(this.options) : + require('yargs')(process.argv.slice(2)); if (typeof this.usage === 'string') { yargs.usage(this.usage) } @@ -83,3 +85,7 @@ Argv.prototype.loadArgv = function () { this.readOnly = true; return this.store; }; + +function isYargs(obj) { + return (typeof obj === 'function') && ('argv' in obj); +}