commander.js 是node.js命令行界面的完整解決方案
命令行
根據選項Options的格式,可分爲簡短形式和完整形式:
根據選項Options的功能,可分爲switch形式和flag形式
Demo
var program = require('commander')
program
.version('0.1.0')
.usage('<command> <arg> [options]')
program
.command('order [food]')
.option('-t, --tip <tip>', 'Add tip','No Tip')
.option('-s, --spicy', 'is Spicy')
.action(function(food, { tip, spicy }) {
//do some thing here
console.log("你有新的外賣訂單:")
console.log("食物:", food)
console.log(spicy? '要辣' : '不要辣')
console.log("備註:", tip)
})
program.parse(process.argv)
文檔
https://github.com/tj/commander.js
http://tj.github.io/commander.js/
https://www.cnblogs.com/mirandachen/p/9826886.html(中文)
更多
結合package.json中的bin字段,即可自己實現一個易用的命令行工具,並可通過npm publish進行發佈,提供給其他人使用