如何用nodejs 开发一个命令行交互工具

摘要:
参考地址1参考地址2一、npmpackage.jsonbin1、package.json{"name":"test","version":"1.0.0","description":"","main":"index.js","scripts":{"test":"echo"Error:notestspecified"&&exit1"},"keywords":[],"author":"","licen

参考地址1

参考地址2

一、npm package.json bin

1、package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bin": {
    "gen": "bin/gen.js"
  },
  "dependencies": {
    "commander": "^2.15.1"
  }
}

2、bin/gen.js

#!/usr/bin/env node
var argv =process.argv;
var filePath =__dirname;
var currentPath =process.cwd();
console.log(argv)
console.log(filePath)
console.log(currentPath)

二、Commnader + inquirer +minimist +download-git-repo + ejs(Nunjucks、handlebars) + execa (child_process)

Commander 示例

#!/usr/bin/env node
var program = require('commander');
 program
   .version('0.0.1')
   .option('-C, --chdir <path>', 'change the working directory')
   .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
   .option('-T, --no-tests', 'ignore test hook')
   .option('-p, --peppers', 'Add peppers')
   .option('-P, --pineapple', 'Add pineapple')
   .option('-b, --bbq-sauce', 'Add bbq sauce')
   .option('--p, --fuck-you', 'fuckyou')
   .option('build --env <fuckyou>', 'fuckyou2')
   .parse(process.argv);
if (program.peppers) console.log('  - peppers');
if (program.pineapple) console.log('  - pineapple');
if (program.bbqSauce) console.log('  - bbq');
if (program.fuckYou) console.log('fuckyou');
if (program.env && program.args.length != 0) {
    console.log(program.args);
}
 program
   .command('init')
   .description('run remote setup commands')
   .action(function() {
     console.log('setup');
   });
 program
   .command('exec <cmd>')
   .description('run the given remote command')
   .action(function(cmd) {
     console.log('exec "%s"', cmd);
   });
 program
   .command('teardown <dir> [otherDirs...]')
   .description('run teardown commands')
   .action(function(dir, otherDirs) {
     console.log('dir "%s"', dir);
     if(otherDirs) {
       otherDirs.forEach(function(oDir) {
         console.log('dir "%s"', oDir);
       });
     }
   });

免责声明:文章转载自《如何用nodejs 开发一个命令行交互工具》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇初次使用InstallShield的笔记(转)JAR包升级,我们关注啥下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mac 下 ts 环境 配置

1. 打开iterm ,输入 sudo npm install -g typescript 2. 复制 安装的 路径 3. 输入 vim ~/.bash_profile 4.export PATH=${PATH}:/Users/farben/.npm-global/lib/node_modules/typescript/bin/ 5.按esc 退出,:wq...

每日技术:npm模块安装机制

内容来自:https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/22 npm模块安装机制 发出npm install命令 查询node_modules目录之中是否已经存在指定模块 若存在,不再重新安装   若不存在, npm 向register查询模块压缩包的网址 下...

SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务

    一、cmd命令行---进行Windows服务操作 1、安装服务 sc create 服务名 binPath= "C:UsersAdministratorDesktopwin32srvDemowin32srvdemoDebugwin32srvDemo.exe" 注:服务名:指创建的Windows服务名        binPath:指Windows...

node爬虫爬取中文时乱码问题 | nodejs gb2312、GBK中文乱码解决方法

iconv需要依赖native库,这样一来,在一些不支持native模块安装的虚拟主机和windows平台上,我们还是无法安心处理GBK编码。 老外写了一个通过纯Javascript转换编码的模块 iconv-lite 可以实现window下的转换 ,通过npm可以安装此模块,bufferhelper是一个操作buffer的加强类 首先安装 npm ins...

Debian 9 启动后进入命令行

打开 default grub 配置 $ sudo vi /etc/default/grub 修改以下3处内容 1. 找到行 GRUB_CMDLINE_LINUX_DEFAULT="quiet",加#注释掉这一行配置,修改为 #GRUB_CMDLINE_LINUX_DEFAULT="quiet" 2. 找到行 GRUB_CMDLINE_LINUX="",修...

CMD命令行正确显示中文

命令行中如果想正确显示UTF-8字符,可以按照以下步骤操作: 1、打开CMD.exe命令行窗口 2、通过 chcp命令改变代码页,UTF-8的代码页为65001 chcp 65001 执行该操作后,代码页就被变成UTF-8了。但是,在窗口中仍旧不能正确显示UTF-8字符。 3、修改窗口属性,改变字体 在命令行标题栏上点击右键,选择"属性"->...