Commander.js——模具行业从业者必备知识手册
-
<h2>Commander.js</h2>
<p><a href="https://api.travis-ci.org/tj/commander.js.svg?branch=master"><img src="http://travis-ci.org/tj/commander.js" alt="Build Status</a>"></p>
<p><a href="upload://ycssy0jEccPEmH8C3gZRrKMZAom.svg"><img src="https://www.npmjs.org/package/commander" alt="NPM Version</a>"></p>
<p><a href="upload://mF8yDNPeD4ziKbXSq5OEepnK4wo.svg"><img src="https://npmcharts.com/compare/commander?minimal=true" alt="NPM Downloads</a>"></p>
<p><a href="https://packagephobia.now.sh/badge?p=commander"><img src="https://packagephobia.now.sh/result?p=commander" alt="Install Size</a>"></p>
<p><a href="upload://iAIY16dpQcK4uPfXQJKuIKHvFBe.svg"><img src="https://gitter.im/tj/commander.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" alt="Join the chat at https://gitter.im/tj/commander.js</a>"></p><p> The complete solution for <a href="http://nodejs.org">node.js</a> command-line interfaces, inspired by Ruby's <a href="https://github.com/commander-rb/commander">commander</a>. </p>
<p> <a href="http://tj.github.com/commander.js/">API documentation</a></p><h3>Installation</h3>
<p> $ npm install commander</p>
<h3>Option parsing</h3>
<p>Options with commander are defined with the <code>.option()</code> method, also serving as documentation for the options. The example below parses args and options from <code>process.argv</code>, leaving remaining args as the <code>program.args</code> array which were not consumed by options.</p>
<pre><code>
#!/usr/bin/env node/**
- Module dependencies.
*/
var program = require('commander');
program
.version('0.1.0')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq-sauce', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);console.log('you ordered a pizza with:');
if (program.peppers) console.log(' - peppers');
if (program.pineapple) console.log(' - pineapple');
if (program.bbqSauce) console.log(' - bbq');
console.log(' - %s cheese', program.cheese);
</code></pre><p>Short flags may be passed as a single arg, for example <code>-abc</code> is equivalent to <code>-a -b -c</code>. Multi-word options such as "--template-engine" are camel-cased, becoming <code>program.templateEngine</code> etc.</p>
<p>Note that multi-word options starting with <code>--no</code> prefix negate the boolean value of the following word. For example, <code>--no-sauce</code> sets the value of <code>program.sauce</code> to false.</p>
<pre><code>
#!/usr/bin/env node/**
- Module dependencies.
*/
var program = require('commander');
program
.option('--no-sauce', 'Remove sauce')
.parse(process.argv);console.log('you ordered a pizza');
if (program.sauce) console.log(' with sauce');
else console.log(' without sauce');
</code></pre><p>To get string arguments from options you will need to use angle brackets <> for required inputs or square brackets [] for optional inputs. </p>
<p>e.g. <code>
</code>.option('-m --myarg [myVar]', 'my super cool description')<code></code></p><p>Then to access the input if it was passed in.</p>
<p>e.g. <code>
</code>var myInput = program.myarg<code></code></p><p><strong>NOTE</strong>: If you pass a argument without using brackets the example above will return true and not the value passed in.</p>
<h3>Version option</h3>
<p>Calling the <code>version</code> implicitly adds the <code>-V</code> and <code>--version</code> options to the command.</p>
<p>When either of these options is present, the command prints the version number and exits.</p><p> $ ./examples/pizza -V</p>
<p> 0.0.1</p><p>If you want your program to respond to the <code>-v</code> option instead of the <code>-V</code> option, simply pass custom flags to the <code>version</code> method using the same syntax as the <code>option</code> method.</p>
<pre><code>
program
.version('0.0.1', '-v, --version')
</code></pre><p>The version flags can be named anything, but the long option is required.</p>
<h3>Command-specific options</h3>
<p>You can a</p>
<hr>
<p>各位师傅觉得这个方案怎么样?有更好的做法吗?</p> - Module dependencies.