replace optimist with yargs
This commit is contained in:
parent
d335b5a0f5
commit
80ec01b91d
9 changed files with 97 additions and 98 deletions
|
@ -61,7 +61,7 @@ The output will be:
|
||||||
|
|
||||||
Configuration management can get complicated very quickly for even trivial applications running in production. `nconf` addresses this problem by enabling you to setup a hierarchy for different sources of configuration with no defaults. **The order in which you attach these configuration sources determines their priority in the hierarchy.** Lets take a look at the options available to you
|
Configuration management can get complicated very quickly for even trivial applications running in production. `nconf` addresses this problem by enabling you to setup a hierarchy for different sources of configuration with no defaults. **The order in which you attach these configuration sources determines their priority in the hierarchy.** Lets take a look at the options available to you
|
||||||
|
|
||||||
1. **nconf.argv(options)** Loads `process.argv` using optimist. If `options` is supplied it is passed along to optimist.
|
1. **nconf.argv(options)** Loads `process.argv` using yargs. If `options` is supplied it is passed along to yargs.
|
||||||
2. **nconf.env(options)** Loads `process.env` into the hierarchy.
|
2. **nconf.env(options)** Loads `process.env` into the hierarchy.
|
||||||
3. **nconf.file(options)** Loads the configuration data at options.file into the hierarchy.
|
3. **nconf.file(options)** Loads the configuration data at options.file into the hierarchy.
|
||||||
4. **nconf.defaults(options)** Loads the data in options.store into the hierarchy.
|
4. **nconf.defaults(options)** Loads the data in options.store into the hierarchy.
|
||||||
|
@ -159,11 +159,11 @@ A simple in-memory storage engine that stores a nested JSON representation of th
|
||||||
```
|
```
|
||||||
|
|
||||||
### Argv
|
### Argv
|
||||||
Responsible for loading the values parsed from `process.argv` by `optimist` into the configuration hierarchy. See the [optimist option docs](https://github.com/substack/node-optimist/#optionskey-opt) for more on the option format.
|
Responsible for loading the values parsed from `process.argv` by `yargs` into the configuration hierarchy. See the [yargs option docs](https://github.com/bcoe/yargs#optionskey-opt) for more on the option format.
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
//
|
//
|
||||||
// Can optionally also be an object literal to pass to `optimist`.
|
// Can optionally also be an object literal to pass to `yargs`.
|
||||||
//
|
//
|
||||||
nconf.argv({
|
nconf.argv({
|
||||||
"x": {
|
"x": {
|
||||||
|
|
|
@ -348,8 +348,8 @@ other values for this instance.</p> </td> <td class="cod
|
||||||
<span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span><span class="p">(</span><span class="s1">'overrides'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_argv</span> <span class="p">});</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <h3>@argv {boolean}</h3>
|
<span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span><span class="p">(</span><span class="s1">'overrides'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_argv</span> <span class="p">});</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <h3>@argv {boolean}</h3>
|
||||||
|
|
||||||
<p>Gets or sets a property indicating if we should wrap calls to <code>.get</code>
|
<p>Gets or sets a property indicating if we should wrap calls to <code>.get</code>
|
||||||
by checking <code>optimist.argv</code>. Can be a boolean or the pass-thru
|
by checking <code>yargs.argv</code>. Can be a boolean or the pass-thru
|
||||||
options for <code>optimist</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineSetter__</span><span class="p">(</span><span class="s1">'argv'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span> <span class="nx">updateSystem</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="s1">'argv'</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span> <span class="p">});</span>
|
options for <code>yargs</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineSetter__</span><span class="p">(</span><span class="s1">'argv'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span> <span class="nx">updateSystem</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="s1">'argv'</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span> <span class="p">});</span>
|
||||||
<span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span><span class="p">(</span><span class="s1">'argv'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_argv</span> <span class="p">});</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <h3>@env {boolean}</h3>
|
<span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span><span class="p">(</span><span class="s1">'argv'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_argv</span> <span class="p">});</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <h3>@env {boolean}</h3>
|
||||||
|
|
||||||
<p>Gets or sets a property indicating if we should wrap calls to <code>.get</code>
|
<p>Gets or sets a property indicating if we should wrap calls to <code>.get</code>
|
||||||
|
|
|
@ -62,10 +62,10 @@ into this instance.</p> </td> <td class="code">
|
||||||
<span class="nx">argv</span><span class="p">;</span>
|
<span class="nx">argv</span><span class="p">;</span>
|
||||||
|
|
||||||
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="k">this</span><span class="p">.</span><span class="nx">argv</span> <span class="o">===</span> <span class="s1">'object'</span><span class="p">)</span> <span class="p">{</span>
|
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="k">this</span><span class="p">.</span><span class="nx">argv</span> <span class="o">===</span> <span class="s1">'object'</span><span class="p">)</span> <span class="p">{</span>
|
||||||
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'optimist'</span><span class="p">).</span><span class="nx">options</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">argv</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'yargs'</span><span class="p">).</span><span class="nx">options</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">argv</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
||||||
<span class="p">}</span>
|
<span class="p">}</span>
|
||||||
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">argv</span><span class="p">)</span> <span class="p">{</span>
|
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">argv</span><span class="p">)</span> <span class="p">{</span>
|
||||||
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'optimist'</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'yargs'</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
||||||
<span class="p">}</span>
|
<span class="p">}</span>
|
||||||
|
|
||||||
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">argv</span><span class="p">)</span> <span class="p">{</span>
|
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">argv</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
|
|
@ -42,15 +42,15 @@ Argv.prototype.loadSync = function () {
|
||||||
//
|
//
|
||||||
Argv.prototype.loadArgv = function () {
|
Argv.prototype.loadArgv = function () {
|
||||||
var self = this,
|
var self = this,
|
||||||
optimist, argv;
|
yargs, argv;
|
||||||
|
|
||||||
optimist = typeof this.options === 'object'
|
yargs = typeof this.options === 'object'
|
||||||
? require('optimist')(process.argv.slice(2)).options(this.options)
|
? require('yargs')(process.argv.slice(2)).options(this.options)
|
||||||
: require('optimist')(process.argv.slice(2));
|
: require('yargs')(process.argv.slice(2));
|
||||||
|
|
||||||
if (typeof this.usage === 'string') { optimist.usage(this.usage) }
|
if (typeof this.usage === 'string') { yargs.usage(this.usage) }
|
||||||
|
|
||||||
argv = optimist.argv
|
argv = yargs.argv
|
||||||
|
|
||||||
if (!argv) {
|
if (!argv) {
|
||||||
return;
|
return;
|
||||||
|
@ -61,8 +61,8 @@ Argv.prototype.loadArgv = function () {
|
||||||
self.set(key, argv[key]);
|
self.set(key, argv[key]);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.showHelp = optimist.showHelp
|
this.showHelp = yargs.showHelp
|
||||||
this.help = optimist.help
|
this.help = yargs.help
|
||||||
|
|
||||||
this.readOnly = true;
|
this.readOnly = true;
|
||||||
return this.store;
|
return this.store;
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "~0.9.0",
|
"async": "~0.9.0",
|
||||||
"ini": "1.x.x",
|
"ini": "1.x.x",
|
||||||
"optimist": "~0.6.0"
|
"yargs": "~3.15.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"vows": "0.8.x"
|
"vows": "0.8.x"
|
||||||
|
@ -28,4 +28,3 @@
|
||||||
"node": ">= 0.4.0"
|
"node": ">= 0.4.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
test/fixtures/scripts/nconf-argv.js
vendored
2
test/fixtures/scripts/nconf-argv.js
vendored
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* default-argv.js: Test fixture for using optimist defaults with nconf.
|
* default-argv.js: Test fixture for using yargs defaults with nconf.
|
||||||
*
|
*
|
||||||
* (C) 2011, Charlie Robbins and the Contributors.
|
* (C) 2011, Charlie Robbins and the Contributors.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* nconf-hierarchical-file-argv.js: Test fixture for using optimist defaults and a file store with nconf.
|
* nconf-hierarchical-file-argv.js: Test fixture for using yargs defaults and a file store with nconf.
|
||||||
*
|
*
|
||||||
* (C) 2011, Charlie Robbins and the Contributors.
|
* (C) 2011, Charlie Robbins and the Contributors.
|
||||||
* (C) 2011, Sander Tolsma
|
* (C) 2011, Sander Tolsma
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* nconf-hierarchical-load-save.js: Test fixture for using optimist, envvars and a file store with nconf.
|
* nconf-hierarchical-load-save.js: Test fixture for using yargs, envvars and a file store with nconf.
|
||||||
*
|
*
|
||||||
* (C) 2011, Charlie Robbins and the Contributors.
|
* (C) 2011, Charlie Robbins and the Contributors.
|
||||||
*
|
*
|
||||||
|
|
2
test/fixtures/scripts/provider-argv.js
vendored
2
test/fixtures/scripts/provider-argv.js
vendored
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* provider-argv.js: Test fixture for using optimist defaults with nconf.
|
* provider-argv.js: Test fixture for using yargs defaults with nconf.
|
||||||
*
|
*
|
||||||
* (C) 2011, Charlie Robbins and the Contributors.
|
* (C) 2011, Charlie Robbins and the Contributors.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue