分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

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

发布时间:2023-09-06 02:00责任编辑:胡小海关键词:jsnodejs

参考地址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 nodevar 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 nodevar 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 开发一个命令行交互工具

原文地址:https://www.cnblogs.com/CyLee/p/9189849.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved