node生成自定义命令(yargs/commander)
第一部分可以生成一个自定义命令,例如常见的”express”,yargs和commander则可以在生成的自定义命令上做扩展,yargs将命令扩展成类似express --l xx
的形式;而commander则可以扩展成类似 ‘express install xx’形式,也可以扩展成express -e xx
的形式,前者写法简单,后者扩展性更好。
- 生成自定义命令
- yargs
- commander
- 完整例子
生成自定义命令
- 新建文件夹
test
,并进入; - 执行
npm init
生成package.json
文件; - 同级目录下新建
hello js
,内容如下:
#! /usr/bin/env node
'use strict';
console.log('123');
- 在
package.json
里添加内容"bin": {"hello": "hello.js"}
:
//package.json
{
"name": "hello",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {"hello": "hello.js"},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
执行命令
npm link
;命令行输入
hello
可看到效果 ——————123;
yargs
yargs所实现的功能是可以根据用户命令行输入选项参数的不同而达到改变node环境变量修改全局变量global,从而实现个性定制的功能
//index.js
const argv = require('yargs').argv;
if (argv.l == 'zh-cn') {
console.log('Chinese site!')
} else if (argv.l == 'en') {
console.log('English site!')
}
//命令行
node yangs.js --l=zh-cn //Chinese site!
node yangs.js --l=en //English site!
Commander
生成自定义命令里将文件的启动转换为一个自定义命令,而commander则可以在自定义命令基础上做命令的扩展(带参数等);
API
整体代码自上到下执行,如果没有碰到parse,则其前面的options不会触发,所以要注意process.xx
的写法。- version(‘0.0.1’) 版本号;
- usage(‘zhao’) 名字
- description(‘hello ,I\’m zhao’) 描述
- allowUnknownOption 取消接收到未定义option时报错的机制,不报错;
- command(‘ab’) 定义子命令;
- alias(‘a’) 定义子命令的短命令;
- option(‘-p, –peppers’,’Add oeooers’) 自定义选项参数和描述
- action(cb) 回调
- parse(process.argv); 至于末尾,解析命令行输入的命令;
总结
我在command,action和option,program.xx这两个组合里绕了很多弯路,总结如下
#!/usr/bin/env node 'use strict'; const program = require('commander'); program
.version('0.0.1')
.usage('例子')
.description('this is a lizi of commander') program
.command('hello [st]')
.action(function(st,value){
hello(st,value);
}) function hello(val,o){
console.log(val);
console.log(1);
console.log(o)
} program
.option('-s --save [value]','保存') program.parse(process.argv); if (program.save){
console.log(program.save);
}- 保持两个组合各自独立,command/action组合,option/program.xx组合,
命令行中不能同时出现command子命令和option选项,即lizi hello 23 -s 25
是错误的; - 当尝试
lizi -s 23
在命令行输出true而非值的时候,是没有在长选项后跟上[xx]
; program.s
可能会有三种情况出现,一是当输入其他可选项的时候,当尝试lizi -t (xx)
时,program.s
为undefined
;二是当输入lizi -s
时,program.s
为默认值,不设置默认值则为true;三是当输入lizi -s xx
时,program.s
为xx;
- 保持两个组合各自独立,command/action组合,option/program.xx组合,
//package.json
{
"name": "lizi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"lizi1":"lizi1.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0"
}
}
//lizi1.js
#!/usr/bin/env node
'use strict';
console.log(1);
const argv = require('yargs').argv;
if (argv.l == 'zh-cn') {
console.log('Chinese site!')
} else if (argv.l == 'en') {
console.log('English site!')
}
例子2: commander和生成自定义命令的组合
//package.json
{
"name": "lizi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"lizi":"lizi.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0"
}
}
//lizi.js
#!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.usage('例子')
.description('this is a lizi of commander')
program
.command('hello [st]')
.action(function(st,value){
hello(st,value);
})
function hello(val,o){
console.log(val);
console.log(1);
console.log(o)
}
program
.option('-f --flag [value]','保存','ha')
.option('-t --tale [value]','保存')
program.parse(process.argv);
if (program.flag){
global.flag = program.flag;
}
console.log(global.flag);
node生成自定义命令(yargs/commander)的更多相关文章
- 【AMAD】django-extensions -- Django框架的自定义命令扩展集合
动机 简介 个人评分 动机 使用Django进行开发的时候,会不会感觉开发工具少了一些.比如每次进入python shell调试的时候要重新import每个model. 简介 django-exten ...
- 如何使用Node.js编写命令工具——以vue-cli为例
vue-cli全局安装之后,提供了vue命令和vue init.vue list.vue build三个子命令,通过命令可以搭建基于vue.js的脚手架项目.本文简单介绍一下这些命令是如何实现的. v ...
- 通过用 .NET 生成自定义窗体设计器来定制应用程序
通过用 .NET 生成自定义窗体设计器来定制应用程序 https://www.microsoft.com/china/MSDN/library/netFramework/netframework/Cu ...
- 3.Node.js 自定义微信菜单
文章目录: 1.Node.js 接入微信公众平台开发 2.Node.js access_token的获取.存储及更新 3.Node.js 自定义微信菜单 ...
- python生成linux命令行工具
您是否也曾一直想生成类似cd, cat等小巧/迷人/实用的小工具作为系统命令或者将python程序打包为exe进行分发?ok,机会来了.利用python 的argparse 和 pyinstaller ...
- linux_shell自定义命令
一.命令可执行文件所在目录 shell命令可执行文件所在目录是保存在环境变量PATH中的,终端输入如下命令查看 PATH 环境变量的内容: $ echo $PATH 我的linux输出如下: /opt ...
- TensorFlow进阶(六)---模型保存与恢复、自定义命令行参数
模型保存与恢复.自定义命令行参数. 在我们训练或者测试过程中,总会遇到需要保存训练完成的模型,然后从中恢复继续我们的测试或者其它使用.模型的保存和恢复也是通过tf.train.Saver类去实现,它主 ...
- 如何用Git.io来生成自定义后缀名的短网址
如何用Git.io来生成自定义后缀名的短网址 git.io是Github的官方短网址,它是用来缩短Github上项目的网址. 效果:Git.io/wacsh将会跳转到https://xhemj.git ...
- 编写 Dockerfile 生成自定义镜像
一般情况下我们可以从公共渠道诸如 DockerHub 获取镜像上获取镜像,但是在实际生产过程中,往往需要定制化的镜像,例如修改一些配置文件,增加一些特殊的命令或软件等需求,这时就需要通过编写 Dock ...
随机推荐
- 解决adb server端口被占用的问题
先执行adb nodaemon server ,查看adb server的端口是多少 C:\Users\xxxx>adb nodaemon server cannot bind 'tcp:5 ...
- zookeeper节点Watch机制实例展示
znode以某种方式发生变化时,“观察”(watch)机制可以让客户端得到通知.可以针对ZooKeeper服务的“操作”来设置观察,该服务的其他 操作可以触发观察. 实现Watcher,复写proce ...
- 深入了解STL中set与hash_set,hash表基础
一,set和hash_set简介 在STL中,set是以红黑树(RB-Tree)作为底层数据结构的,hash_set是以哈希表(Hash table)作为底层数据结构的.set可以在时间复杂度为O(l ...
- VS2010安装失败解决办法
1. 运行regedit打开注册表: 2. 找到HKEY_LOCAL_MACHINE\SOFWARE\ Microsoft\ Internet Explorer\ MAIN: 3. MAIN子键的权限 ...
- FFrpc python客户端lib
摘要: Ffrpc可以很方便的构建c++ server, 在网游服务器程序开发中,进程间通讯非常的重要,比如gateserver和gameserver或dbserver之间的通信.而ffrpc可以使得 ...
- 关于mvc、webapi中get、post、put、delete的参数
webapi中post提交的数据必须是一个类,get提交的数量无所谓 多种参数get时,参数名不能相同: 在能通过c#的校验的前提下,参数名.参数数量不能全完相同 public string Get( ...
- centos7.0 手动编译 lamp环境
首先新建用户 lamper,并添加 sodu权限 两种方法:is not in the sudoers file 解决(转) xx is not in the sudoers file 问题解决[转载 ...
- 修改windows密码后ssrs报错
昨夜修改了windows的登录密码,第二日发现ssrs全部无法访问.显示filenotfound等错误.细想一下,应该是修改了windows的密码导致ssrs权限验证失败. 因此将ssrs的服务帐号修 ...
- 解决play-1.4.0在linux或mac下提示No such file or directory的问题
问题原因:"play"脚本中有特殊符号. 解决方案:写脚本去掉即可. 代码:fixplay.py 放在play-1.4.0目录下执行.亲测在osx与ubuntu下均可用. with ...
- 使用ssh免密码登录其他机器
本机 ssh-keygen -t rsa – cd ~/ssh – cp -p id_rsa.pub authorized_keys2 – chmod go-rwx authorized_keys2 ...