Node.js 命令行程序开发教程 ---------http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html
五、yargs 模块
shelljs 只解决了如何调用 shell 命令,而 yargs 模块能够解决如何处理命令行参数。它也需要安装。
$ npm install --save yargs
yargs 模块提供 argv 对象,用来读取命令行参数。请看改写后的 hello 。
#!/usr/bin/env node
var argv = require('yargs').argv; console.log('hello ', argv.name);
使用时,下面两种用法都可以。
$ hello --name=tom
hello tom $ hello --name tom
hello tom
也就是说,process.argv 的原始返回值如下。
$ node hello --name=tom
[ 'node',
'/path/to/myscript.js',
'--name=tom' ]
yargs 可以上面的结果改为一个对象,每个参数项就是一个键值对。
var argv = require('yargs').argv; // $ node hello --name=tom
// argv = {
// name: tom
// };
如果将 argv.name 改成 argv.n,就可以使用一个字母的短参数形式了。
$ hello -n tom
hello tom
可以使用 alias 方法,指定 name 是 n 的别名。
#!/usr/bin/env node
var argv = require('yargs')
.alias('n', 'name')
.argv; console.log('hello ', argv.n);
这样一来,短参数和长参数就都可以使用了。
$ hello -n tom
hello tom
$ hello --name tom
hello tom
argv 对象有一个下划线(_)属性,可以获取非连词线开头的参数。
#!/usr/bin/env node
var argv = require('yargs').argv; console.log('hello ', argv.n);
console.log(argv._);
用法如下。
$ hello A -n tom B C
hello tom
[ 'A', 'B', 'C' ]
六、命令行参数的配置
yargs 模块还提供3个方法,用来配置命令行参数。
- demand:是否必选
- default:默认值
- describe:提示
#!/usr/bin/env node
var argv = require('yargs')
.demand(['n'])
.default({n: 'tom'})
.describe({n: 'your name'})
.argv; console.log('hello ', argv.n);
上面代码指定 n 参数不可省略,默认值为 tom,并给出一行提示。
options 方法允许将所有这些配置写进一个对象。
#!/usr/bin/env node
var argv = require('yargs')
.option('n', {
alias : 'name',
demand: true,
default: 'tom',
describe: 'your name',
type: 'string'
})
.argv; console.log('hello ', argv.n);
有时,某些参数不需要值,只起到一个开关作用,这时可以用 boolean 方法指定这些参数返回布尔值。
#!/usr/bin/env node
var argv = require('yargs')
.boolean(['n'])
.argv; console.log('hello ', argv.n);
上面代码中,参数 n 总是返回一个布尔值,用法如下。
$ hello
hello false
$ hello -n
hello true
$ hello -n tom
hello true
boolean 方法也可以作为属性,写入 option 对象。
#!/usr/bin/env node
var argv = require('yargs')
.option('n', {
boolean: true
})
.argv; console.log('hello ', argv.n);
七、帮助信息
yargs 模块提供以下方法,生成帮助信息。
- usage:用法格式
- example:提供例子
- help:显示帮助信息
- epilog:出现在帮助信息的结尾
#!/usr/bin/env node
var argv = require('yargs')
.option('f', {
alias : 'name',
demand: true,
default: 'tom',
describe: 'your name',
type: 'string'
})
.usage('Usage: hello [options]')
.example('hello -n tom', 'say hello to Tom')
.help('h')
.alias('h', 'help')
.epilog('copyright 2015')
.argv; console.log('hello ', argv.n);
执行结果如下。
$ hello -h Usage: hello [options] Options:
-f, --name your name [string] [required] [default: "tom"]
-h, --help Show help [boolean] Examples:
hello -n tom say hello to Tom copyright 2015
八、子命令
yargs 模块还允许通过 command 方法,设置 Git 风格的子命令。
#!/usr/bin/env node
var argv = require('yargs')
.command("morning", "good morning", function (yargs) {
console.log("Good Morning");
})
.command("evening", "good evening", function (yargs) {
console.log("Good Evening");
})
.argv; console.log('hello ', argv.n);
用法如下。
$ hello morning -n tom
Good Morning
hello tom
可以将这个功能与 shellojs 模块结合起来。
#!/usr/bin/env node
require('shelljs/global');
var argv = require('yargs')
.command("morning", "good morning", function (yargs) {
echo("Good Morning");
})
.command("evening", "good evening", function (yargs) {
echo("Good Evening");
})
.argv; console.log('hello ', argv.n);
每个子命令往往有自己的参数,这时就需要在回调函数中单独指定。回调函数中,要先用 reset 方法重置 yargs 对象。
#!/usr/bin/env node
require('shelljs/global');
var argv = require('yargs')
.command("morning", "good morning", function (yargs) {
echo("Good Morning");
var argv = yargs.reset()
.option("m", {
alias: "message",
description: "provide any sentence"
})
.help("h")
.alias("h", "help")
.argv; echo(argv.m);
})
.argv;
用法如下。
$ hello morning -m "Are you hungry?"
Good Morning
Are you hungry?
九、其他事项
(1)返回值
根据 Unix 传统,程序执行成功返回 0,否则返回 1 。
if (err) {
process.exit(1);
} else {
process.exit(0);
}
(2)重定向
Unix 允许程序之间使用管道重定向数据。
$ ps aux | grep 'node'
脚本可以通过监听标准输入的data 事件,获取重定向的数据。
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
process.stdout.write(data);
});
下面是用法。
$ echo 'foo' | ./hello
hello foo
(3)系统信号
操作系统可以向执行中的进程发送信号,process 对象能够监听信号事件。
process.on('SIGINT', function () {
console.log('Got a SIGINT');
process.exit(0);
});
发送信号的方法如下。
$ kill -s SIGINT [process_id]
(完)
Node.js 命令行程序开发教程 ---------http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html的更多相关文章
- Node.js 命令行程序开发教程
nodejs开发命令行程序非常方便,具体操作方式查看下面几篇文章 http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html ...
- Node.js 命令行程序开发资料
Node.js 命令行程序开发教程http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html用Node.js创建命令行工具ht ...
- Node.js 命令行工具的编写
日常开发中,编写 Node.js 命令行工具来完成一些小任务是很常见的操作.其编写也不难,和日常编写 Node.js 代码并无二致. package.json 中的 bin 字段 一个 npm 模块, ...
- 一个最简单 node.js 命令行工具
一个最简单 node.js 命令行工具 node.js cli $ node cli.js xyz # OR $ node cli xyz 接受参数 process.argv js "use ...
- nodejs模块发布及命令行程序开发
前置技能 npm工具为nodejs提供了一个模块和管理程序模块依赖的机制,当我们希望把模块贡献出去给他人使用时,可以把我们的程序发布到npm提供的公共仓库中,为了方便模块的管理,npm规定要使用一个叫 ...
- [编译] 5、在Linux下搭建安卓APP的开发烧写环境(makefile版)—— 在Linux上用命令行+VIM开发安卓APP
星期三, 19. 九月 2018 02:19上午 - BEAUTIFULZZZZ 0)前言 本文不讨论用IDE和文本编辑器开发的优劣,是基于以下两点考虑去尝试用命令行编译安卓APP的: 了解安卓APP ...
- Node: 开发命令行程序
CLI 的全称是 Command-line Interface (命令行界面),即在命令行接受用户的键盘输入并作出响应和执行的程序. 在 Node.js 中,全局安装的包一般都具有命令行界面的功能,例 ...
- 如何用node编写命令行工具,附上一个ginit示例,并推荐好用的命令行工具
原文 手把手教你写一个 Node.js CLI 强大的 Node.js 除了能写传统的 Web 应用,其实还有更广泛的用途.微服务.REST API.各种工具……甚至还能开发物联网和桌面应用.Java ...
- 如何用Node编写命令行工具
0. 命令行工具 当全局安装模块之后,我们可以在控制台下执行指定的命令来运行操作,如果npm一样.我把这样的模块称之为命令行工具模块(如理解有偏颇,欢迎指正) 1.用Node编写命令行工具 在Node ...
随机推荐
- FAST_START_MTTR_TARGET
Release 9i introduced a new parameter, FAST_START_MTTR_TARGET, that makes controlling instance recov ...
- QT如何修改编程语言的字体
工具-选项,然后在文本编辑器中设置要的字体
- LeetCode 246. Strobogrammatic Number (可颠倒数字) $
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- postgis经常使用函数介绍(一)
概述: 在进行地理信息系统开发的过程中,经常使用的空间数据库有esri的sde,postgres的postgis以及mySQL的mysql gis等等,在本文.给大家介绍的是有关postgis的一些经 ...
- tiny4412学习(四)之移植linux-设备树(1)设备树基础知识及GPIO中断【转】
本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/74177978 版权声明:本文为博主原创文章,转载请注明http://blog.c ...
- mvn scope (转)
策略一: 对于容器提供的(如:servlet-api-2.3等)和测试需要时的(如:junit-3.81等),可以直接在pom.xml中去掉. maven的dependency中有一个tag是< ...
- Kindergarten Election
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3715 题意:有N个孩子投票选举leader,不能自己选自己.Sheldon ...
- bzoj3224 普通平衡树(splay 模板)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11427 Solved: 4878[Submit][St ...
- Android Jetpack - 使用 Navigation 管理页面跳转
在今年的 IO 大会上,发布了一套叫 Android Jetpack 的程序库.Android Jetpack 里的组件大部分我们都接触过了,其中也有一些全新的组件,其中一个就是 Navigation ...
- mysql 数据去重
update ptop_investrecord set delflag = 1 where cid = 250 and uid = 92569 and delflag = 0 and progr ...