Skip to content

Commit

Permalink
Add posibility to pass a yargs instance to argv() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Deveaud authored and mhamann committed Oct 28, 2017
1 parent 856fdf8 commit 3e26bb2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions lib/nconf/stores/argv.js
Expand Up @@ -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) }

Expand Down Expand Up @@ -83,3 +85,7 @@ Argv.prototype.loadArgv = function () {
this.readOnly = true;
return this.store;
};

function isYargs(obj) {
return (typeof obj === 'function') && ('argv' in obj);
}

0 comments on commit 3e26bb2

Please sign in to comment.