vorpal 又一个方便的cli 开发包
vorpal 是一个npm 包,我们可以用来开发专业的cli 程序
简单使用
- 初始化项目
yarn init -y
- 添加依赖
yarn add vorpal
- 简单demo
app.js
// const vorpal = require("vorpal")();
// vorpal.show()
const vorpal = require('vorpal')();
vorpal
.command('say [words...]')
.option('-b, --backwards')
.option('-t, --twice')
.action(function (args, callback) {
let str = args.words.join(' ');
str = (args.options.backwards) ?
str.split('').reverse().join('') :
str;
this.log(str);
callback();
})
vorpal
.command('sayapp [words...]')
.action(function (args, cb) {
this.log(args.words.join(' '));
cb();
});
vorpal
.command('reverse [words...]')
.action(function (args, cb) {
this.log(args.stdin.split('').reverse().join(''));
cb();
});
vorpal
.command('color [color] [text...]')
.autocomplete(['2018-12-31', '2019-01-03', '2019-03-01'])
.action(function (args, cb) {
this.log(args)
this.log(vorpal.chalk[args.color](args.text.join('')));
cb();
});
vorpal
.command('order pizza')
.option('--anchovies')
.action(function (args, cb) {
const self = this;
this.prompt({
type: 'input',
name: 'time',
message: 'When would you like your pizza?'
}, function (result) {
self.log(`Okay, ${result.time} it is!`);
cb();
});
});
vorpal.delimiter('dalongrong$').
show()
- 运行效果
node app.js
node app.js
dalongrong$
dalongrong$ ls
Invalid Command. Showing Help:
Commands:
help [command
exit Exits application.
say [options] [words
sayapp [words
reverse [words
color [color] [text
order pizza [options]
dalongrong$
参考资料
https://github.com/dthree/vorpal
https://developer.telerik.com/featured/creating-node-js-command-line-utilities-improve-workflow/
https://github.com/rongfengliang/vorpal-pkg-demo