欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

詳解Node.js命令行程序開發(fā)教程

一種編程語言是否易用,很大程度上,取決于開發(fā)命令行程序的能力。

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設、成都做網(wǎng)站與策劃設計,清河網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設10年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:清河等地區(qū)。清河做網(wǎng)站價格咨詢:18982081108

Node.js 作為目前最熱門的開發(fā)工具之一,怎樣使用它開發(fā)命令行程序,是 Web 開發(fā)者應該掌握的技能。

下面就是我在它的基礎上擴展的教程,應該是目前最好的解決方案了。

一、可執(zhí)行腳本

我們從最簡單的講起。

首先,使用 JavaScript 語言,寫一個可執(zhí)行腳本 hello 。

#!/usr/bin/env node
console.log('hello world');

然后,修改 hello 的權限。

$ chmod 755 hello

現(xiàn)在,hello 就可以執(zhí)行了。

$ ./hello
hello world

如果想把 hello 前面的路徑去除,可以將 hello 的路徑加入環(huán)境變量 PATH。但是,另一種更好的做法,是在當前目錄下新建 package.json ,寫入下面的內(nèi)容。

{
 "name": "hello",
 "bin": {
  "hello": "hello"
 }
}

然后執(zhí)行 npm link 命令。

$ npm link

現(xiàn)在再執(zhí)行 hello ,就不用輸入路徑了。

$ hello
hello world

二、命令行參數(shù)的原始寫法

命令行參數(shù)可以用系統(tǒng)變量 process.argv 獲取。

下面是一個腳本 hello 。

#!/usr/bin/env node
console.log('hello ', process.argv[2]);

執(zhí)行時,直接在腳本文件后面,加上參數(shù)即可。

$ ./hello tom
hello tom

上面代碼中,實際上執(zhí)行的是 node ./hello tom ,對應的 process.argv 是 ['node', '/path/to/hello', 'tom'] 。

三、新建進程

腳本可以通過 child_process 模塊新建子進程,從而執(zhí)行 Unix 系統(tǒng)命令。

#!/usr/bin/env node
var name = process.argv[2];
var exec = require('child_process').exec;

var child = exec('echo hello ' + name, function(err, stdout, stderr) {
 if (err) throw err;
 console.log(stdout);
});

用法如下。

$ ./hello tom
hello tom

四、shelljs 模塊

shelljs 模塊重新包裝了 child_process,調用系統(tǒng)命令更加方便。它需要安裝后使用。

npm install --save shelljs

然后,改寫腳本。

#!/usr/bin/env node
var name = process.argv[2];
var shell = require("shelljs");

shell.exec("echo hello " + name);

上面代碼是 shelljs 的本地模式,即通過 exec 方法執(zhí)行 shell 命令。此外還有全局模式,允許直接在腳本中寫 shell 命令。

require('shelljs/global');

if (!which('git')) {
 echo('Sorry, this script requires git');
 exit(1);
}

mkdir('-p', 'out/Release');
cp('-R', 'stuff/*', 'out/Release');

cd('lib');
ls('*.js').forEach(function(file) {
 sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
 sed('-i', /.*REMOVE_THIS_LINE.*\n/, '', file);
 sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file);
});
cd('..');

if (exec('git commit -am "Auto-commit"').code !== 0) {
 echo('Error: Git commit failed');
 exit(1);
}

五、yargs 模塊

shelljs 只解決了如何調用 shell 命令,而 yargs 模塊能夠解決如何處理命令行參數(shù)。它也需要安裝。

$ npm install --save yargs

yargs 模塊提供 argv 對象,用來讀取命令行參數(shù)。請看改寫后的 hello 。

#!/usr/bin/env node
var argv = require('yargs').argv;

console.log('hello ', argv.name);

使用時,下面兩種用法都可以。

$ hello --name=tom
hello tom

$ hello --name tom
hello tom

也就是說,process.argv 的原始返回值如下。

$ node hello --name=tom
[ 'node',
 '/path/to/myscript.js',
 '--name=tom' ]

yargs 可以上面的結果改為一個對象,每個參數(shù)項就是一個鍵值對。

var argv = require('yargs').argv;

// $ node hello --name=tom
// argv = {
//  name: tom
// };

如果將 argv.name 改成 argv.n,就可以使用一個字母的短參數(shù)形式了。

$ hello -n tom
hello tom

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

#!/usr/bin/env node
var argv = require('yargs')
 .alias('n', 'name')
 .argv;

console.log('hello ', argv.n);

這樣一來,短參數(shù)和長參數(shù)就都可以使用了。

$ hello -n tom
hello tom
$ hello --name tom
hello tom

argv 對象有一個下劃線(_)屬性,可以獲取非連詞線開頭的參數(shù)。

#!/usr/bin/env node
var argv = require('yargs').argv;

console.log('hello ', argv.n);
console.log(argv._);

用法如下。

$ hello A -n tom B C
hello tom
[ 'A', 'B', 'C' ]

六、命令行參數(shù)的配置

yargs 模塊還提供3個方法,用來配置命令行參數(shù)。

  1. demand:是否必選
  2. default:默認值
  3. describe:提示
#!/usr/bin/env node
var argv = require('yargs')
 .demand(['n'])
 .default({n: 'tom'})
 .describe({n: 'your name'})
 .argv;

console.log('hello ', argv.n);

上面代碼指定 n 參數(shù)不可省略,默認值為 tom,并給出一行提示。

options 方法允許將所有這些配置寫進一個對象。

#!/usr/bin/env node
var argv = require('yargs')
 .option('n', {
  alias : 'name',
  demand: true,
  default: 'tom',
  describe: 'your name',
  type: 'string'
 })
 .argv;

console.log('hello ', argv.n);

有時,某些參數(shù)不需要值,只起到一個開關作用,這時可以用 boolean 方法指定這些參數(shù)返回布爾值。

#!/usr/bin/env node
var argv = require('yargs')
 .boolean(['n'])
 .argv;

console.log('hello ', argv.n);

上面代碼中,參數(shù) n 總是返回一個布爾值,用法如下。

$ hello
hello false
$ hello -n
hello true
$ hello -n tom
hello true

boolean 方法也可以作為屬性,寫入 option 對象。

#!/usr/bin/env node
var argv = require('yargs')
 .option('n', {
  boolean: true
 })
 .argv;

console.log('hello ', argv.n);

七、幫助信息

yargs 模塊提供以下方法,生成幫助信息。

  1. usage:用法格式
  2. example:提供例子
  3. help:顯示幫助信息
  4. epilog:出現(xiàn)在幫助信息的結尾
#!/usr/bin/env node
var argv = require('yargs')
 .option('f', {
  alias : 'name',
  demand: true,
  default: 'tom',
  describe: 'your name',
  type: 'string'
 })
 .usage('Usage: hello [options]')
 .example('hello -n tom', 'say hello to Tom')
 .help('h')
 .alias('h', 'help')
 .epilog('copyright 2015')
 .argv;

console.log('hello ', argv.n);

執(zhí)行結果如下。

$ hello -h

Usage: hello [options]

Options:
 -f, --name your name [string] [required] [default: "tom"]
 -h, --help Show help [boolean]

Examples:
 hello -n tom say hello to Tom

copyright 2015

八、子命令

yargs 模塊還允許通過 command 方法,設置 Git 風格的子命令。

#!/usr/bin/env node
var argv = require('yargs')
 .command("morning", "good morning", function (yargs) {
  console.log("Good Morning");
 })
 .command("evening", "good evening", function (yargs) {
  console.log("Good Evening");
 })
 .argv;

console.log('hello ', argv.n);

用法如下。

$ hello morning -n tom
Good Morning
hello tom

可以將這個功能與 shellojs 模塊結合起來。

#!/usr/bin/env node
require('shelljs/global');
var argv = require('yargs')
 .command("morning", "good morning", function (yargs) {
  echo("Good Morning");
 })
 .command("evening", "good evening", function (yargs) {
  echo("Good Evening");
 })
 .argv;

console.log('hello ', argv.n);

每個子命令往往有自己的參數(shù),這時就需要在回調函數(shù)中單獨指定。回調函數(shù)中,要先用 reset 方法重置 yargs 對象。

#!/usr/bin/env node
require('shelljs/global');
var argv = require('yargs')
 .command("morning", "good morning", function (yargs) { 
  echo("Good Morning");
  var argv = yargs.reset()
   .option("m", {
    alias: "message",
    description: "provide any sentence"
   })
   .help("h")
   .alias("h", "help")
   .argv;

  echo(argv.m);
 })
 .argv;

用法如下。

$ hello morning -m "Are you hungry?"
Good Morning
Are you hungry?

九、其他事項

(1)返回值

根據(jù) Unix 傳統(tǒng),程序執(zhí)行成功返回 0,否則返回 1 。

if (err) {
 process.exit(1);
} else {
 process.exit(0);
}

(2)重定向

Unix 允許程序之間使用管道重定向數(shù)據(jù)。

$ ps aux | grep 'node'

腳本可以通過監(jiān)聽標準輸入的data 事件,獲取重定向的數(shù)據(jù)。

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
 process.stdout.write(data);
});

下面是用法。

$ echo 'foo' | ./hello
hello foo

(3)系統(tǒng)信號

操作系統(tǒng)可以向執(zhí)行中的進程發(fā)送信號,process 對象能夠監(jiān)聽信號事件。

process.on('SIGINT', function () {
 console.log('Got a SIGINT');
 process.exit(0);
});

發(fā)送信號的方法如下。

$ kill -s SIGINT [process_id]

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當前標題:詳解Node.js命令行程序開發(fā)教程
網(wǎng)站地址:http://chinadenli.net/article8/jgjdip.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、App設計微信公眾號、網(wǎng)站排名網(wǎng)站導航、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設
日本加勒比系列在线播放| 国产丝袜美女诱惑一区二区| 日韩欧美一区二区亚洲| 最新69国产精品视频| 欧美一级日韩中文字幕| 国产欧美一区二区三区精品视| 国产在线一区二区免费| 国产女同精品一区二区| 女人精品内射国产99| 自拍偷女厕所拍偷区亚洲综合| 最好看的人妻中文字幕| 一区二区福利在线视频| 亚洲专区一区中文字幕| 亚洲一区二区三区在线中文字幕| 国产伦精品一区二区三区高清版| 国产av天堂一区二区三区粉嫩| 色一情一乱一区二区三区码| 91久久精品国产一区蜜臀| 91后入中出内射在线| 色欧美一区二区三区在线| 日韩综合国产欧美一区| 国产欧美亚洲精品自拍| 国产精品偷拍一区二区| 日本人妻中出在线观看| 不卡在线播放一区二区三区| 蜜桃传媒视频麻豆第一区| 国产一区二区精品丝袜 | 成人精品视频在线观看不卡| 国产又大又黄又粗又免费| 亚洲高清亚洲欧美一区二区| 日本加勒比中文在线观看| 国产又粗又深又猛又爽又黄| 国产日韩在线一二三区| 老鸭窝老鸭窝一区二区| 1024你懂的在线视频| 亚洲黄香蕉视频免费看| 国产精品香蕉免费手机视频| 日韩欧美一区二区不卡视频| 国产欧美日韩在线一区二区| 国产丝袜女优一区二区三区| 日本久久中文字幕免费|