Socket
Socket
Sign inDemoInstall

hemsl

Package Overview
Dependencies
1
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hemsl

A lightweight Node.js command line argv parser and command executor


Version published
Maintainers
1
Install size
216 kB
Created

Readme

Source

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)

参数解析

ParamTypeDefaultDescription
configObject配置对象
[config.__]Booleanfalse是否停止解析--后面的内容
[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

ParamTypeDefaultDescription
[argv]Arrayprocess.argv.slice(2)要解析的参数数组
[execute]Booleanfalse是否自动执行参数中的命令

args.execute() ⇒ Args

执行命令

Kind: instance method of Args
Access: public

args.option(key, config) ⇒ Args

添加全局选项

Kind: instance method of Args
Access: public

ParamTypeDescription
keyString选项名称
configObject选项配置

args.command(cmd, config) ⇒ Command

添加命令

Kind: instance method of Args
Access: public

ParamTypeDescription
cmdString命令名称
configObject命令配置

args.help([cmdName]) ⇒ Args

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

Kind: instance method of Args
Access: public

ParamTypeDescription
[cmdName]String命令名称

args.version(ver) ⇒ Args

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

Kind: instance method of Args
Access: public

ParamTypeDescription
verString版本号

args.bin(binName) ⇒ Args

设置App的命令名称

Kind: instance method of Args
Access: public

ParamTypeDescription
binNameString名称

Command

Kind: global class

new Command(cmd, config)

创建命令

ParamTypeDescription
cmdString命令名称
configObject配置参数
config.usageString命令使用帮助
config.describeString命令描述信息
config.fnfunction执行命令时调用的函数
config.optionsObject命令支持的选项(option)

command.option(key, opt) ⇒ Command

为命令创建一个选项

Kind: instance method of Command
Access: public

ParamTypeDescription
keyString选项名称
optObject选项配置

Keywords

FAQs

Last updated on 26 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc