模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    Commander.js——模具行业从业者必备知识手册

    社区公告
    1
    1
    0
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <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>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors