hemsl

1.2.7 • Public • Published

hemsl

hemsl is a lightweight Node.js command line argv parser and command executor

Build Status Build status codecov js-semistandard-style npm npm

White flowers Komatsu, scientific name: Villadia batesii (Hemsl.) Baehni & Macbride, Sedum, Tulian perennial multi-meat plants, flowering generally 4 to 5 months. White flowers Komatsu leaves more beautiful, there is a certain ornamental value; potted plants can be placed on the TV, the computer can absorb radiation, can also be planted in the room to absorb formaldehyde and other substances, purify the air.

Install

Use npm

$ npm install hemsl --save

Use yarn

$ yarn add hemsl

Usages

Create an instance

var Args = require('hemsl');
var args = new Args();

Set app version and bin name

args
  .version('1.1.0')
  .bin('example');

Add global option

args
  .option(, {
    option: 'debug',
    default: true,
    describe: 'Print debug log (global option)',
    alias: 'd'
  })
  .option({
    option: 'grep <expression>',
    default: true,
    describe: 'Debug log filter (global option)',
    alias: 'g'
  });

Add command

args.command({
  command: 'start <port> [ip]',
  describe: 'Start a local server',
  usage: 'example start <port> [ip] [options]',
  /**
    * The `start` command handle
    * Start a local server
    */
  fn: function(port, ip){
    console.log('Server started at', 'http://' + ip + ':' + port);
 
    var http = require('http');
 
    var server = http.createServer(function(req, res){
        console.log(req.method.bold.gray, req.url);
        res.end(req.url);
    })
 
    server.listen(port, ip);
  }
})

Add option to command

There are two ways to add an option to a command:

  • Method 1: Call the args.command() method and set the configuration field options
  • Method 2: Call the args.command().option() method

Method 1

args.command({
  command: 'start <port> [ip]',
  describe: '...',
  usage: '...',
  fn: function(port, ip){
      //...
  },
  options: {
    'p': {
      default: '',
      describe: 'service port',
      alias: 'port',
      usage: ''
    },
    'hot-reload': {
      alias: 'H',
      describe: 'enable hot reload'
    }
  }
});

Method 2

args.command({
  command: 'start <port> [ip]', 
  // ...
})
.option({
  option: 'date-format', 
  default: 'yyyy-MM-dd',
  alias: 'R',
  describe: 'date format string'
})
.option({
  option: 'time-format',  
  alias: 'm',
  default: 'HH:mm:ss',
  describe: 'time format string'
})

Other Example

API

Classes

Args
Command

Args

Kind: global class

new Args(config)

参数解析

Param Type Default Description
config Object 配置对象
[config.__] Boolean false 是否停止解析--后面的内容
[config.colors] Object 文本颜色配置
[config.colors.title] String 'white' 标题文本颜色
[config.colors.command] String 'white' 命令名称文本颜色
[config.colors.option] String 'white' Option文本颜色
[config.colors.paragraph] String 'gray' 段落文本颜色
[config.colors.parameter] String 'gray' 参数文本颜色

args.parse([argv], [execute]) ⇒ Object

解析参数,返回解析后的参数对象。如果参数executetrue,自动执行argv中的命令

Kind: instance method of Args
Returns: Object - 解析后的对象
Access: public

Param Type Default Description
[argv] Array process.argv.slice(2) 要解析的参数数组
[execute] Boolean false 是否自动执行参数中的命令

args.execute() ⇒ Args

执行命令

Kind: instance method of Args
Access: public

args.option(key, config) ⇒ Args

添加全局选项

Kind: instance method of Args
Access: public

Param Type Description
key String 选项名称
config Object 选项配置

args.command(cmd, config) ⇒ Command

添加命令

Kind: instance method of Args
Access: public

Param Type Description
cmd String 命令名称
config Object 命令配置

args.help([cmdName]) ⇒ Args

显示自动生成的帮助信息,如果指定了命令名称,则显示对应命令的帮助信息

Kind: instance method of Args
Access: public

Param Type Description
[cmdName] String 命令名称

args.version(ver) ⇒ Args

设置App版本号,默认值为1.0.0。这个版本号会在全局-v/--version的时候显示

Kind: instance method of Args
Access: public

Param Type Description
ver String 版本号

args.bin(binName) ⇒ Args

设置App的命令名称

Kind: instance method of Args
Access: public

Param Type Description
binName String 名称

Command

Kind: global class

new Command(cmd, config)

创建命令

Param Type Description
cmd String 命令名称
config Object 配置参数
config.usage String 命令使用帮助
config.describe String 命令描述信息
config.fn function 执行命令时调用的函数
config.options Object 命令支持的选项(option)

command.option(key, opt) ⇒ Command

为命令创建一个选项

Kind: instance method of Command
Access: public

Param Type Description
key String 选项名称
opt Object 选项配置

Readme

Keywords

Package Sidebar

Install

npm i hemsl

Weekly Downloads

86

Version

1.2.7

License

MIT

Unpacked Size

180 kB

Total Files

26

Last publish

Collaborators

  • zhaoxingyue
  • zhangyuang
  • langshu
  • zdying