Node.js开发命令行程序

可执行脚本

hello.js

#!/usr/bin/env node
console.log('Hello World!')

修改 hello.js 的权限

chmod 755 hello.js

执行 hello.js

./hello.js

// 以下是错误的   
// hello.js
// ./hello
// hello    

新建package.json

{
  "name": "hello-world",
  "version": "1.0.0", 
  "bin": {
      "runhello": "hello.js"
  }
}

将当前目录模块安装到全局,一定要有”version”才可以

sudo npm install . -g

也可以执行npm link命令添加全局的symbolic link

sudo npm link

这时,在任何目录下运行runhello都可以。

也可以删除

sudo npm uninstall hello-world -g

命令行参数

修改 hello.js,重新全局安装

#!/usr/bin/env node
console.log(process.argv)

执行

WeiHeLi:hello weiheli$ runhello
[ '/usr/local/bin/node', '/usr/local/bin/runhello' ]

WeiHeLi:hello weiheli$ runhello arg1 arg2
[ '/usr/local/bin/node',
  '/usr/local/bin/runhello',
  'arg1',
  'arg2' ]

从上面可以看出,第一个参数是process.argv[2]


yargs 模块

使用内置的process.agrv比较麻烦,commander模块和yargs模块可以帮助我们简化命令行开发。yargs更好用。

安装 yargs

npm install --save yargs

修改 hello.js,重新全局安装

#!/usr/bin/env node
var argv = require('yargs').argv

console.log('hello ', argv.name)

有两种执行方式

$ runhello --name weiheli
hello  weiheli

$ runhello --name=weiheli
hello  weiheli

(1) 短参数形式

一个字母的短参数形式。修改 hello.js,然后全局安装

#!/usr/bin/env node
var argv = require('yargs').argv

console.log('hello ', argv.n)

有四种执行方式

$ runhello -n weiheli
hello  weiheli

$ runhello -n=weiheli
hello  weiheli

$ runhello --n weiheli
hello  weiheli

$ runhello --n=weiheli
hello  weiheli

(2) 指定别名

可以使用alias方法,指定别名。修改 hello.js,然后重新全局安装

#!/usr/bin/env node
var argv = require('yargs')
    .alias('n', 'name')
    .argv

console.log('hello ', argv.name)

以下六种执行方式都可以

$ hello --name weiheli

$ hello --name=weiheli

$ hello --n weiheli

$ hello --n=weiheli

$ hello -n weiheli

$ hello -n=weiheli

(3) 非连词线开头的参数

argv对象有一个下划线(_)属性,可以获取非连词线开头的参数。修改 hello.js,然后重新全局安装

#!/usr/bin/env node
var argv = require('yargs')
    .alias('n', 'name')
    .argv

console.log('hello ', argv.name)
console.log(argv._)

执行

$ runhello A -name weiheli B C

hello  true
[ 'A', 'B', 'C' ]

(4) 命令行参数的配置

yargs模块还提供3个方法,用来配置命令行参数。

  • demand:是否必选
  • default:默认值
  • describe:提示

修改 hello.js,然后重新全局安装

#!/usr/bin/env node
var argv = require('yargs')
    .demand(['n'])
    .default({n: 'weiheli'})
    .describe({n: 'You Name'})
    .argv

console.log('hello ', argv.n)

执行

$ runhello
hello  weiheli

$ runhello -n weiheli2
hello  weiheli2

如果需要约束多个参数

#!/usr/bin/env node
var argv = require('yargs')
    .demand(['n'])
    .default({n: 'weiheli'})
    .describe({n: 'You Name'})
    .demand(['a'])
    .default({a: '26'})
    .describe({a: 'You Age'})
    .argv

console.log('hello ', argv.n, ' ', argv.a)

执行

$ runhello
hello  weiheli   26

options/option方法允许将所有这些配置写进一个对象

#!/usr/bin/env node
var argv = require('yargs')
    .option('n', {
        alias: 'name',
        demand: true,
        default: 'weiheli',
        describe: 'You Name',
        type: 'string'
    })
    .option('a', {
        demand: true,
        default: '26',
        describe: 'You Aame',
        type: 'string'
    })
    .argv

console.log('hello ', argv.n, ' ', argv.a)

执行

$ runhello
hello  weiheli   26

有时,某些参数不需要值,只起到一个开关作用,这时可以用 boolean 方法指定这些参数返回布尔值。

#!/usr/bin/env node
var argv = require('yargs')
    .boolean(['n'])
    .argv

console.log('hello ', argv.n)

执行

$ runhello
hello  false

$ runhello -n
hello  true

boolean方法也可以作为属性,写入option对象。

#!/usr/bin/env node
var argv = require('yargs')
    .option('n', {
        boolean: true
    })
    .argv

console.log('hello ', argv.n)

执行

$ runhello
hello  false

$ runhello -n
hello  true

(5) 帮助信息

yargs模块提供以下方法,生成帮助信息。

  • usage:用法格式
  • example:提供例子
  • help:显示帮助信息
  • epilog:出现在帮助信息的结尾

修改 hello.js

#!/usr/bin/env node
var argv = require('yargs')
    .option('n', {
        alias: 'name',
        demand: true,
        default: 'weiheli',
        describe: 'You Name',
        type: 'string'
    })
    .usage('Usage: runhello [options]')
    .example('runhello -n tom', 'say hello to Tom')
    .help('h')
    .alias('h', 'help')
    .epilog('Copyright 2016')
    .argv

console.log('hello ', argv.n)

执行

$ runhello -h
Usage: runhello [options]

选项:
  -n, --name  You Name                   [string] [required] [默认值: "weiheli"]
  -h, --help  显示帮助信息                                             [boolean]

示例:
  runhello -n tom  say hello to Tom

Copyright 2016

你可能感兴趣的:(Node.js)