node生成自定義命令(yargs/commander)


 

第一部分可以生成一個自定義命令,例如常見的”express”,yargs和commander則可以在生成的自定義命令上做擴展,yargs將命令擴展成類似express --l xx的形式;而commander則可以擴展成類似 ‘express install xx’形式,也可以擴展成express -e xx的形式,前者寫法簡單,后者擴展性更好。

 

  1. 生成自定義命令
  2. yargs
  3. commander
  4. 完整例子

生成自定義命令

  1. 新建文件夾test,並進入;
  2. 執行npm init 生成package.json文件;
  3. 同級目錄下新建hello js,內容如下:
 #! /usr/bin/env node

'use strict'
;
console.log('123');
  1. package.json里添加內容"bin": {"hello": "hello.js"}:
//package.json

{
"name": "hello",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {"hello": "hello.js"},

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
  1. 執行命令npm link;

  2. 命令行輸入hello可看到效果 ——————123;

yargs

yargs所實現的功能是可以根據用戶命令行輸入選項參數的不同而達到改變node環境變量修改全局變量global,從而實現個性定制的功能

//index.js
const argv = require('yargs').argv;
if (argv.l == 'zh-cn') {
console.log('Chinese site!')
} else if (argv.l == 'en') {
console.log('English site!')
}
//命令行
node yangs.js --l=zh-cn //Chinese site!
node yangs.js --l=en //English site!

Commander

生成自定義命令里將文件的啟動轉換為一個自定義命令,而commander則可以在自定義命令基礎上做命令的擴展(帶參數等);

  1. API 
    整體代碼自上到下執行,如果沒有碰到parse,則其前面的options不會觸發,所以要注意process.xx的寫法。

    • version(‘0.0.1’) 版本號;
    • usage(‘zhao’) 名字
    • description(‘hello ,I\’m zhao’) 描述
    • allowUnknownOption 取消接收到未定義option時報錯的機制,不報錯;
    • command(‘ab’) 定義子命令;
    • alias(‘a’) 定義子命令的短命令;
    • option(‘-p, –peppers’,’Add oeooers’) 自定義選項參數和描述
    • action(cb) 回調
    • parse(process.argv); 至於末尾,解析命令行輸入的命令;
  2. 總結 
    我在command,action和option,program.xx這兩個組合里繞了很多彎路,總結如下


    #!/usr/bin/env node


    'use strict'
    ;

    const program = require('commander');

    program
    .version('0.0.1')
    .usage('例子')
    .description('this is a lizi of commander')

    program
    .command('hello [st]')
    .action(function(st,value){
    hello(st,value);
    })

    function hello(val,o){
    console.log(val);
    console.log(1);
    console.log(o)
    }

    program
    .option('-s --save [value]','保存')

    program.parse(process.argv);

    if (program.save){
    console.log(program.save);
    }

    • 保持兩個組合各自獨立,command/action組合,option/program.xx組合, 
      命令行中不能同時出現command子命令和option選項,即lizi hello 23 -s 25是錯誤的;
    • 當嘗試lizi -s 23在命令行輸出true而非值的時候,是沒有在長選項后跟上[xx];
    • program.s可能會有三種情況出現,一是當輸入其他可選項的時候,當嘗試lizi -t (xx)時,program.sundefined;二是當輸入lizi -s時,program.s為默認值,不設置默認值則為true;三是當輸入lizi -s xx時,program.s為xx;

兩個完整例子

注意:寫好下面代碼在執行前需要npm link 綁定關系;

例子1: yargs和生成自定義命令的組合

//package.json
{
"name": "lizi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"lizi1":"lizi1.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0"
}
}

//lizi1.js
#!/usr/bin/env node

'use strict'
;

console.log(1);

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

if (argv.l == 'zh-cn') {
console.log('Chinese site!')
} else if (argv.l == 'en') {
console.log('English site!')
}

例子2: commander和生成自定義命令的組合

//package.json
{
"name": "lizi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"lizi":"lizi.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0"
}
}


//lizi.js

#!/usr/bin/env node

'use strict'
;

const program = require('commander');

program
.version('0.0.1')
.usage('例子')
.description('this is a lizi of commander')

program
.command('hello [st]')
.action(function(st,value){
hello(st,value);
})

function hello(val,o){
console.log(val);
console.log(1);
console.log(o)
}

program
.option('-f --flag [value]','保存','ha')
.option('-t --tale [value]','保存')

program.parse(process.argv);

if (program.flag){
global.flag = program.flag;
}

console.log(global.flag);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM