Node.js 命令行程序开发教程

一种编程语言是否易用,很大程度上,取决于开发命令行程序的能力。

Node.js 作为目前最热门的开发工具之一,怎样使用它开发命令行程序,是 Web 开发者应该掌握的技能。

Node.js 命令行程序开发教程

最近,Npm的网志有一组系列文章,我觉得写得非常好。下面就是我在它的基础上扩展的教程,应该是目前最好的解决方案了。

一、可执行脚本

我们从最简单的讲起。

首先,使用 JavaScript 语言,写一个可执行脚本 hello 。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello world'</span><span class="pun">);</span>

然后,修改 hello 的权限。

  1. <span class="pln">$ chmod </span><span class="lit">755</span><span class="pln"> hello</span>

现在,hello 就可以执行了。

  1. <span class="pln">$ </span><span class="pun">./</span><span class="pln">hello</span>
  2. <span class="pln">hello world</span>

如果想把 hello 前面的路径去除,可以将 hello 的路径加入环境变量 PATH。但是,另一种更好的做法,是在当前目录下新建 package.json ,写入下面的内容。

  1. <span class="pun">{</span>
  2. <span class="str">"name"</span><span class="pun">:</span><span class="str">"hello"</span><span class="pun">,</span>
  3. <span class="str">"bin"</span><span class="pun">:</span><span class="pun">{</span>
  4. <span class="str">"hello"</span><span class="pun">:</span><span class="str">"hello"</span>
  5. <span class="pun">}</span>
  6. <span class="pun">}</span>

然后执行 npm link 命令。

  1. <span class="pln">$ npm link</span>

现在再执行 hello ,就不用输入路径了。

  1. <span class="pln">$ hello</span>
  2. <span class="pln">hello world</span>

二、命令行参数的原始写法

命令行参数可以用系统变量 process.argv 获取。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> process</span><span class="pun">.</span><span class="pln">argv</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]);</span>

执行时,直接在脚本文件后面,加上参数即可。

  1. <span class="pln">$ </span><span class="pun">./</span><span class="pln">hello tom</span>
  2. <span class="pln">hello tom</span>

三、新建进程

脚本可以通过 child_process 模块新建子进程,从而执行 Unix 系统命令。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> process</span><span class="pun">.</span><span class="pln">argv</span><span class="pun">[</span><span class="lit">2</span><span class="pun">];</span>
  3. <span class="kwd">var</span><span class="kwd">exec</span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'child_process'</span><span class="pun">).</span><span class="kwd">exec</span><span class="pun">;</span>
  4. <span class="kwd">var</span><span class="pln"> child </span><span class="pun">=</span><span class="kwd">exec</span><span class="pun">(</span><span class="str">'echo hello '</span><span class="pun">+</span><span class="pln"> name</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">err</span><span class="pun">,</span><span class="pln"> stdout</span><span class="pun">,</span><span class="pln"> stderr</span><span class="pun">)</span><span class="pun">{</span>
  5. <span class="kwd">if</span><span class="pun">(</span><span class="pln">err</span><span class="pun">)</span><span class="kwd">throw</span><span class="pln"> err</span><span class="pun">;</span>
  6. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">stdout</span><span class="pun">);</span>
  7. <span class="pun">});</span>

用法如下。

  1. <span class="pln">$ </span><span class="pun">./</span><span class="pln">hello tom</span>
  2. <span class="pln">hello tom</span>

四、shelljs 模块

shelljs 模块重新包装了 child_process,调用系统命令更加方便。它需要安装后使用。

  1. <span class="pln">npm install </span><span class="pun">--</span><span class="pln">save shelljs</span>

然后,改写脚本。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> process</span><span class="pun">.</span><span class="pln">argv</span><span class="pun">[</span><span class="lit">2</span><span class="pun">];</span>
  3. <span class="kwd">var</span><span class="pln"> shell </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">"shelljs"</span><span class="pun">);</span>
  4. <span class="pln">shell</span><span class="pun">.</span><span class="kwd">exec</span><span class="pun">(</span><span class="str">"echo hello "</span><span class="pun">+</span><span class="pln"> name</span><span class="pun">);</span>

上面代码是 shelljs 的本地模式,即通过 exec 方法执行 shell 命令。此外还有全局模式,允许直接在脚本中写 shell 命令。

  1. <span class="kwd">require</span><span class="pun">(</span><span class="str">'shelljs/global'</span><span class="pun">);</span>
  2. <span class="kwd">if</span><span class="pun">(!</span><span class="pln">which</span><span class="pun">(</span><span class="str">'git'</span><span class="pun">))</span><span class="pun">{</span>
  3. <span class="pln">echo</span><span class="pun">(</span><span class="str">'Sorry, this script requires git'</span><span class="pun">);</span>
  4. <span class="kwd">exit</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span>
  5. <span class="pun">}</span>
  6. <span class="pln">mkdir</span><span class="pun">(</span><span class="str">'-p'</span><span class="pun">,</span><span class="str">'out/Release'</span><span class="pun">);</span>
  7. <span class="pln">cp</span><span class="pun">(</span><span class="str">'-R'</span><span class="pun">,</span><span class="str">'stuff/*'</span><span class="pun">,</span><span class="str">'out/Release'</span><span class="pun">);</span>
  8. <span class="pln">cd</span><span class="pun">(</span><span class="str">'lib'</span><span class="pun">);</span>
  9. <span class="pln">ls</span><span class="pun">(</span><span class="str">'*.js'</span><span class="pun">).</span><span class="pln">forEach</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">file</span><span class="pun">)</span><span class="pun">{</span>
  10. <span class="pln">sed</span><span class="pun">(</span><span class="str">'-i'</span><span class="pun">,</span><span class="str">'BUILD_VERSION'</span><span class="pun">,</span><span class="str">'v0.1.2'</span><span class="pun">,</span><span class="pln"> file</span><span class="pun">);</span>
  11. <span class="pln">sed</span><span class="pun">(</span><span class="str">'-i'</span><span class="pun">,</span><span class="str">/.*REMOVE_THIS_LINE.*\n/</span><span class="pun">,</span><span class="str">''</span><span class="pun">,</span><span class="pln"> file</span><span class="pun">);</span>
  12. <span class="pln">sed</span><span class="pun">(</span><span class="str">'-i'</span><span class="pun">,</span><span class="str">/.*REPLACE_LINE_WITH_MACRO.*\n/</span><span class="pun">,</span><span class="pln"> cat</span><span class="pun">(</span><span class="str">'macro.js'</span><span class="pun">),</span><span class="pln"> file</span><span class="pun">);</span>
  13. <span class="pun">});</span>
  14. <span class="pln">cd</span><span class="pun">(</span><span class="str">'..'</span><span class="pun">);</span>
  15. <span class="kwd">if</span><span class="pun">(</span><span class="kwd">exec</span><span class="pun">(</span><span class="str">'git commit -am "Auto-commit"'</span><span class="pun">).</span><span class="pln">code </span><span class="pun">!==</span><span class="lit">0</span><span class="pun">)</span><span class="pun">{</span>
  16. <span class="pln">echo</span><span class="pun">(</span><span class="str">'Error: Git commit failed'</span><span class="pun">);</span>
  17. <span class="kwd">exit</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span>
  18. <span class="pun">}</span>

五、yargs 模块

shelljs 只解决了如何调用 shell 命令,而 yargs 模块能够解决如何处理命令行参数。它也需要安装。

  1. <span class="pln">$ npm install </span><span class="pun">--</span><span class="pln">save yargs</span>

yargs 模块提供 argv 对象,用来读取命令行参数。请看改写后的 hello 。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">).</span><span class="pln">argv</span><span class="pun">;</span>
  3. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">name</span><span class="pun">);</span>

使用时,下面两种用法都可以。

  1. <span class="pln">$ hello </span><span class="pun">--</span><span class="pln">name</span><span class="pun">=</span><span class="pln">tom</span>
  2. <span class="pln">hello tom</span>
  3. <span class="pln">$ hello </span><span class="pun">--</span><span class="pln">name tom</span>
  4. <span class="pln">hello tom</span>

如果将 argv.name 改成 argv.n,就可以使用一个字母的短参数形式了。

  1. <span class="pln">$ hello </span><span class="pun">-</span><span class="pln">n tom</span>
  2. <span class="pln">hello tom</span>

可以使用 alias 方法,指定 name 是 n 的别名。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  3. <span class="pun">.</span><span class="kwd">alias</span><span class="pun">(</span><span class="str">'n'</span><span class="pun">,</span><span class="str">'name'</span><span class="pun">)</span>
  4. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  5. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

这样一来,短参数和长参数就都可以使用了。

  1. <span class="pln">$ hello </span><span class="pun">-</span><span class="pln">n tom</span>
  2. <span class="pln">hello tom</span>
  3. <span class="pln">$ hello </span><span class="pun">--</span><span class="pln">name tom</span>
  4. <span class="pln">hello tom</span>

argv 对象有一个下划线(_)属性,可以获取非连词线开头的参数。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">).</span><span class="pln">argv</span><span class="pun">;</span>
  3. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>
  4. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">argv</span><span class="pun">.</span><span class="pln">_</span><span class="pun">);</span>

用法如下。

  1. <span class="pln">$ hello A </span><span class="pun">-</span><span class="pln">n tom B C</span>
  2. <span class="pln">hello tom</span>
  3. <span class="pun">[</span><span class="str">'A'</span><span class="pun">,</span><span class="str">'B'</span><span class="pun">,</span><span class="str">'C'</span><span class="pun">]</span>

六、命令行参数的配置

yargs 模块还提供3个方法,用来配置命令行参数。

  • demand:是否必选
  • default:默认值
  • describe:提示
  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  3. <span class="pun">.</span><span class="pln">demand</span><span class="pun">([</span><span class="str">'n'</span><span class="pun">])</span>
  4. <span class="pun">.</span><span class="kwd">default</span><span class="pun">({</span><span class="pln">n</span><span class="pun">:</span><span class="str">'tom'</span><span class="pun">})</span>
  5. <span class="pun">.</span><span class="pln">describe</span><span class="pun">({</span><span class="pln">n</span><span class="pun">:</span><span class="str">'your name'</span><span class="pun">})</span>
  6. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  7. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

上面代码指定 n 参数不可省略,默认值为 tom,并给出一行提示。

options 方法允许将所有这些配置写进一个对象。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  3. <span class="pun">.</span><span class="pln">option</span><span class="pun">(</span><span class="str">'n'</span><span class="pun">,</span><span class="pun">{</span>
  4. <span class="kwd">alias</span><span class="pun">:</span><span class="str">'name'</span><span class="pun">,</span>
  5. <span class="pln">demand</span><span class="pun">:</span><span class="kwd">true</span><span class="pun">,</span>
  6. <span class="kwd">default</span><span class="pun">:</span><span class="str">'tom'</span><span class="pun">,</span>
  7. <span class="pln">describe</span><span class="pun">:</span><span class="str">'your name'</span><span class="pun">,</span>
  8. <span class="pln">type</span><span class="pun">:</span><span class="str">'string'</span>
  9. <span class="pun">})</span>
  10. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  11. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

有时,某些参数不需要值,只起到一个开关作用,这时可以用 boolean 方法指定这些参数返回布尔值。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  3. <span class="pun">.</span><span class="kwd">boolean</span><span class="pun">([</span><span class="str">'n'</span><span class="pun">])</span>
  4. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  5. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

上面代码中,参数 n 总是返回一个布尔值,用法如下。

  1. <span class="pln">$ hello</span>
  2. <span class="pln">hello </span><span class="kwd">false</span>
  3. <span class="pln">$ hello </span><span class="pun">-</span><span class="pln">n</span>
  4. <span class="pln">hello </span><span class="kwd">true</span>
  5. <span class="pln">$ hello </span><span class="pun">-</span><span class="pln">n tom</span>
  6. <span class="pln">hello </span><span class="kwd">true</span>

boolean 方法也可以作为属性,写入 option 对象。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  3. <span class="pun">.</span><span class="pln">option</span><span class="pun">(</span><span class="str">'n'</span><span class="pun">,</span><span class="pun">{</span>
  4. <span class="kwd">boolean</span><span class="pun">:</span><span class="kwd">true</span>
  5. <span class="pun">})</span>
  6. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  7. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

七、帮助信息

yargs 模块提供以下方法,生成帮助信息。

  • usage:用法格式
  • example:提供例子
  • help:显示帮助信息
  • epilog:出现在帮助信息的结尾
  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  3. <span class="pun">.</span><span class="pln">option</span><span class="pun">(</span><span class="str">'f'</span><span class="pun">,</span><span class="pun">{</span>
  4. <span class="kwd">alias</span><span class="pun">:</span><span class="str">'name'</span><span class="pun">,</span>
  5. <span class="pln">demand</span><span class="pun">:</span><span class="kwd">true</span><span class="pun">,</span>
  6. <span class="kwd">default</span><span class="pun">:</span><span class="str">'tom'</span><span class="pun">,</span>
  7. <span class="pln">describe</span><span class="pun">:</span><span class="str">'your name'</span><span class="pun">,</span>
  8. <span class="pln">type</span><span class="pun">:</span><span class="str">'string'</span>
  9. <span class="pun">})</span>
  10. <span class="pun">.</span><span class="pln">usage</span><span class="pun">(</span><span class="str">'Usage: hello [options]'</span><span class="pun">)</span>
  11. <span class="pun">.</span><span class="pln">example</span><span class="pun">(</span><span class="str">'hello -n tom'</span><span class="pun">,</span><span class="str">'say hello to Tom'</span><span class="pun">)</span>
  12. <span class="pun">.</span><span class="pln">help</span><span class="pun">(</span><span class="str">'h'</span><span class="pun">)</span>
  13. <span class="pun">.</span><span class="kwd">alias</span><span class="pun">(</span><span class="str">'h'</span><span class="pun">,</span><span class="str">'help'</span><span class="pun">)</span>
  14. <span class="pun">.</span><span class="pln">epilog</span><span class="pun">(</span><span class="str">'copyright 2015'</span><span class="pun">)</span>
  15. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  16. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

执行结果如下。

  1. <span class="pln">$ hello </span><span class="pun">-</span><span class="pln">h</span>
  2. <span class="typ">Usage</span><span class="pun">:</span><span class="pln"> hello </span><span class="pun">[</span><span class="pln">options</span><span class="pun">]</span>
  3. <span class="typ">Options</span><span class="pun">:</span>
  4. <span class="pun">-</span><span class="pln">f</span><span class="pun">,</span><span class="pun">--</span><span class="pln">name your name </span><span class="pun">[</span><span class="kwd">string</span><span class="pun">]</span><span class="pun">[</span><span class="pln">required</span><span class="pun">]</span><span class="pun">[</span><span class="kwd">default</span><span class="pun">:</span><span class="str">"tom"</span><span class="pun">]</span>
  5. <span class="pun">-</span><span class="pln">h</span><span class="pun">,</span><span class="pun">--</span><span class="pln">help </span><span class="typ">Show</span><span class="pln"> help </span><span class="pun">[</span><span class="kwd">boolean</span><span class="pun">]</span>
  6. <span class="typ">Examples</span><span class="pun">:</span>
  7. <span class="pln">hello </span><span class="pun">-</span><span class="pln">n tom say hello to </span><span class="typ">Tom</span>
  8. <span class="pln">copyright </span><span class="lit">2015</span>

八、子命令

yargs 模块还允许通过 command 方法,设置 Git 风格的子命令。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  3. <span class="pun">.</span><span class="pln">command</span><span class="pun">(</span><span class="str">"morning"</span><span class="pun">,</span><span class="str">"good morning"</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">yargs</span><span class="pun">)</span><span class="pun">{</span>
  4. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Good Morning"</span><span class="pun">);</span>
  5. <span class="pun">})</span>
  6. <span class="pun">.</span><span class="pln">command</span><span class="pun">(</span><span class="str">"evening"</span><span class="pun">,</span><span class="str">"good evening"</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">yargs</span><span class="pun">)</span><span class="pun">{</span>
  7. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Good Evening"</span><span class="pun">);</span>
  8. <span class="pun">})</span>
  9. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  10. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

用法如下。

  1. <span class="pln">$ hello morning </span><span class="pun">-</span><span class="pln">n tom</span>
  2. <span class="typ">Good</span><span class="typ">Morning</span>
  3. <span class="pln">hello tom</span>

可以将这个功能与 shellojs 模块结合起来。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">require</span><span class="pun">(</span><span class="str">'shelljs/global'</span><span class="pun">);</span>
  3. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  4. <span class="pun">.</span><span class="pln">command</span><span class="pun">(</span><span class="str">"morning"</span><span class="pun">,</span><span class="str">"good morning"</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">yargs</span><span class="pun">)</span><span class="pun">{</span>
  5. <span class="pln">echo</span><span class="pun">(</span><span class="str">"Good Morning"</span><span class="pun">);</span>
  6. <span class="pun">})</span>
  7. <span class="pun">.</span><span class="pln">command</span><span class="pun">(</span><span class="str">"evening"</span><span class="pun">,</span><span class="str">"good evening"</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">yargs</span><span class="pun">)</span><span class="pun">{</span>
  8. <span class="pln">echo</span><span class="pun">(</span><span class="str">"Good Evening"</span><span class="pun">);</span>
  9. <span class="pun">})</span>
  10. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  11. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'hello '</span><span class="pun">,</span><span class="pln"> argv</span><span class="pun">.</span><span class="pln">n</span><span class="pun">);</span>

每个子命令往往有自己的参数,这时就需要在回调函数中单独指定。回调函数中,要先用 reset 方法重置 yargs 对象。

  1. <span class="com">#!/usr/bin/env node</span>
  2. <span class="kwd">require</span><span class="pun">(</span><span class="str">'shelljs/global'</span><span class="pun">);</span>
  3. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="kwd">require</span><span class="pun">(</span><span class="str">'yargs'</span><span class="pun">)</span>
  4. <span class="pun">.</span><span class="pln">command</span><span class="pun">(</span><span class="str">"morning"</span><span class="pun">,</span><span class="str">"good morning"</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">yargs</span><span class="pun">)</span><span class="pun">{</span>
  5. <span class="pln">echo</span><span class="pun">(</span><span class="str">"Good Morning"</span><span class="pun">);</span>
  6. <span class="kwd">var</span><span class="pln"> argv </span><span class="pun">=</span><span class="pln"> yargs</span><span class="pun">.</span><span class="pln">reset</span><span class="pun">()</span>
  7. <span class="pun">.</span><span class="pln">option</span><span class="pun">(</span><span class="str">"m"</span><span class="pun">,</span><span class="pun">{</span>
  8. <span class="kwd">alias</span><span class="pun">:</span><span class="str">"message"</span><span class="pun">,</span>
  9. <span class="pln">description</span><span class="pun">:</span><span class="str">"provide any sentence"</span>
  10. <span class="pun">})</span>
  11. <span class="pun">.</span><span class="pln">help</span><span class="pun">(</span><span class="str">"h"</span><span class="pun">)</span>
  12. <span class="pun">.</span><span class="kwd">alias</span><span class="pun">(</span><span class="str">"h"</span><span class="pun">,</span><span class="str">"help"</span><span class="pun">)</span>
  13. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>
  14. <span class="pln">echo</span><span class="pun">(</span><span class="pln">argv</span><span class="pun">.</span><span class="pln">m</span><span class="pun">);</span>
  15. <span class="pun">})</span>
  16. <span class="pun">.</span><span class="pln">argv</span><span class="pun">;</span>

用法如下。

  1. <span class="pln">$ hello morning </span><span class="pun">-</span><span class="pln">m </span><span class="str">"Are you hungry?"</span>
  2. <span class="typ">Good</span><span class="typ">Morning</span>
  3. <span class="typ">Are</span><span class="pln"> you hungry</span><span class="pun">?</span>

九、其他事项

(1)返回值

根据 Unix 传统,程序执行成功返回 0,否则返回 1 。

  1. <span class="kwd">if</span><span class="pun">(</span><span class="pln">err</span><span class="pun">)</span><span class="pun">{</span>
  2. <span class="pln">process</span><span class="pun">.</span><span class="kwd">exit</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span>
  3. <span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span>
  4. <span class="pln">process</span><span class="pun">.</span><span class="kwd">exit</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span>
  5. <span class="pun">}</span>

(2)重定向

Unix 允许程序之间使用管道重定向数据。

  1. <span class="pln">$ ps aux </span><span class="pun">|</span><span class="pln"> grep </span><span class="str">'node'</span>

脚本可以通过监听标准输入的data 事件,获取重定向的数据。

  1. <span class="pln">process</span><span class="pun">.</span><span class="pln">stdin</span><span class="pun">.</span><span class="pln">resume</span><span class="pun">();</span>
  2. <span class="pln">process</span><span class="pun">.</span><span class="pln">stdin</span><span class="pun">.</span><span class="pln">setEncoding</span><span class="pun">(</span><span class="str">'utf8'</span><span class="pun">);</span>
  3. <span class="pln">process</span><span class="pun">.</span><span class="pln">stdin</span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="str">'data'</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">data</span><span class="pun">)</span><span class="pun">{</span>
  4. <span class="pln">process</span><span class="pun">.</span><span class="pln">stdout</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">data</span><span class="pun">);</span>
  5. <span class="pun">});</span>

下面是用法:

  1. <span class="pln">$ echo </span><span class="str">'foo'</span><span class="pun">|</span><span class="pun">./</span><span class="pln">hello</span>
  2. <span class="pln">hello foo</span>

(3)系统信号

操作系统可以向执行中的进程发送信号,process 对象能够监听信号事件。

  1. <span class="pln">process</span><span class="pun">.</span><span class="pln">on</span><span class="pun">(</span><span class="str">'SIGINT'</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
  2. <span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'Got a SIGINT'</span><span class="pun">);</span>
  3. <span class="pln">process</span><span class="pun">.</span><span class="kwd">exit</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span>
  4. <span class="pun">});</span>

发送信号的方法如下。

  1. <span class="pln">$ kill </span><span class="pun">-</span><span class="pln">s SIGINT </span><span class="pun">[</span><span class="pln">process_id</span><span class="pun">]</span>

下面的内容你可能也喜欢

Node.js 的详细介绍:请点这里
Node.js 的下载地址:请点这里

相关推荐