使用node.js开发命令行工具(一)命令行输入输出UI库

commander.js

node.js命令行开发工具开发库,使node.js开发CLI工具变得简单,允许快捷的定义形如<command> [options]的命令。

基础用法:

const program = require('commander');

 program
  .version(require('../package.json').version, '-v, --version') // 定义版本信息
  .usage('<command> [options]'); // 定义命令用法
 
program
  .command('rm <dir>') // 定义一个rm命令
  .description('删除文件或文件夹') // 给rm命令添加描述信息,获取命令帮助信息的时候会显示
  .option('-r, --recursive', 'Remove recursively') // rm允许添加-r或者--recursive命令进行递归
  .action(function (dir, cmd) { // 对应命令的处理函数
    console.log('remove ' + dir + (cmd.recursive ? ' recursively' : ''))
  });
 
 program.parse(process.argv); // commander的入口欧,传入命令行参数执行解析
复制代码

github仓库:github.com/tj/commande…

inquirer.js

node.js 交互式命令行界面开发库,允许方便的定义使用上下左右进行列表选择等交互式命令。

基础用法:

const inquirer = require('inquirer');

inquirer.prompt(
{
    type: 'input', // 问题类型,包括input,number,confirm,list,rawlist,password
    name: 'name', 
    message: '请输入项目名称', // 问题
    default: 'unnamed' // 默认值
    validate: (input: string) => {
        if (input.length > 255) { // 输入验证:name长度不允许超过255
            return '项目名称超过限制';
        }
        return true;
    }        
},
{
    type: 'list',
    name: 'type',
    message: '请选择',
    choices: ['item1', 'item2', 'item3', 'item4'], // 可选选项
    default: 'project'
}).then(answers => {
    console.log(answers.name);
    console.log(answers.type);
});
复制代码

github仓库: github.com/SBoudrias/I…

ora

优雅的命令行Loading动画。

const ora = require('ora');

const spinner = ora('Loading unicorns').start();

setTimeout(() => {
	spinner.color = 'yellow';
	spinner.text = 'Loading rainbows';
}, 1000);

setTimeout(() => {
	spinner.stop();
}, 2000);
复制代码



github仓库:https://github.com/sindresorhus/ora
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值