npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

npargv

v1.0.9

Published

process argv parser

Downloads

42

Readme

npargv 解析进程的参数

Node.js扩展,用于快速解析process.argv。

安装

npm i npargv

使用示例


'use strict'

const parseArgv = require('npargv')

let ret = parseArgv({
  '--port=' : {
    name: 'port',
    //别名
    alias: '-p',
    type: 'int',
    min: 2000,
    max: 2100
  },

  '--host=' : {
    name: 'host',
    match: /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/i
  },

  '-w' : {
    name: 'worker',
    type: 'int',
    min: 1,
    max: 4,
    //若设置了默认值,则在参数不合法时会自动使用默认值不会返回错误信息。
    default: 2
  },

  '--https': {
    name: 'https'
  },

  '--http2': {
    name: 'http2'
  },

  '--test': {
    name: 'test'
  },

  '--id' : {
    name : 'id',
    callback: (str) => {
      return str.split(',').filter(p => p.length > 0)
    }
  },

  //自动转换为{type: 'bool', name: 'limit'}
  '--limit' : 'limit',

  //自动转换为{type: 'bool', name: '-x'}
  '-x' : false

})

console.log(ret)

运行命令参数:

$ node test.js x --host=1.2.3.4 --https --http2 --test --port=2010 --id 1,2,3,4,5 --local a b c

运行输出结果:

{
  ok: true,
  errmsg: '',
  args: {
    worker: 2,
    https: true,
    http2: true,
    test: true,
    limit: false,
    host: '1.2.3.4',
    port: 2010,
    id: [ '1', '2', '3', '4', '5' ],
    '--local': true,
    '-x': false
  },
  // 不是参数,也不是-开头的会放在list中,方便获取。
  // 如果存在位置引用的参数,则会优先解析,所以x没有在list中。
  list: ['a', 'b', 'c']
}

解析后的结果,若ok为false,则errmsg会给出错误提示信息。若参数无错误,则args是解析后的参数对象。

解析过程中,若参数不在传递的对象描述信息内,仍然会在args中体现,key值即为参数的名字,可以参考示例中的--local。

如果对参数的描述信息不是object类型,则会转换为boolean类型,此时name值即为参数的名字。参考-x参数。

若没有给定type限定范围,则会根据描述自动设定type,这可能会有错误判断,所以尽可能给定type限定。

type推断规则如下:

  • 若参数含有=,比如--port=,则type为string类型。

  • 否则,若描述对象具备match或callback,则为string类型。

  • 否则,若描述对象含有min或max则为int类型。

  • 否则,若描述对象具备default,则如果default是数字或字符串类型,type和default类型一致。

  • 否则,type为bool。

type支持类型如下:

  • int 或 number,整数类型。

  • float,浮点数类型。

  • string,字符串类型。

  • bool或boolean,布尔类型。

参数描述的选项

| 选项 | 是否必须 | 描述 | | ---- | ---- | ---- | | type | 否 | 参数类型,若不给定则会根据描述信息自动设定。 | | name | 否 | 解析后参数的名字,就是解析后参数对象的key值。若不给定则使用参数名称。 | | min | 否 | 限制最小值。 | | max | 否 | 限制最大值。 | | default | 否 | 默认值,若设置,则检测到传递的参数不合法,会采用默认值不会返回错误信息。对于bool来说,default无效。默认值为false。 | | match | 否 | 正则表达式,若给定,会进行正则匹配。 | | callback | 否 | 函数,若给定,会把参数值传递到函数,若函数返回值不是undefined,则作为最后解析的值。 |

autoDefault自动设定默认值

在程序解析过程中,有时候需要必须返回参数,不合法则使用默认值,这需要对每个选项都使用default属性,或者使用@autoDefault让npargv自动设定默认值。


'use strict'

const parseArgv = require('npargv')

let opts = {
  //启用自动设定默认值。
  '@autoDefault' : true,

  //没有设定默认值,会自动设定默认值为min的值。
  '--port' : {
    min: 1234,
    max: 5678
  },

  //没有default设定默认值,类型为数字也没有min的限制,会自动设置为0
  '-x' : {
    type : 'int'
  }


}

let {args} = parseArgv(opts)

自动设定默认值的规则:

  • 若type为bool则默认值为false

  • 若type为string,则默认值为 空字符串。

  • 若type为number、int、float,则默认值为min属性给定的值,若没有min则默认值为0。

第一个参数作为子命令


let argscfg = {
    //定义支持的子命令
    '@command' : [
        'create', 'show', 'update', 'delete'
    ],

    //当不输入时,默认的命令
    '@defaultCommand': 'show'
}

位置引用参数

以 $ 加数字作为key值即表示这是一个位置相关的参数,在解析过程中会先进行位置引用参数的解析。

但是位置的索引数字和实际内部的索引有差异:

  • 索引位置从1开始。

  • 索引位置是参数值,不会包括命令名称。

  • 如果设定了@command,则位置1会自动在解析时从command后开始。

注意:@command表示的是第一个参数,用于脚本的子命令功能。


let {args} = npargv({
    '@autoDefault': true,
    '$1': {
        type: 'string',
    },
    '$2': {
        type: 'string',
        callback: (v) => {
            return ['i', 'o', 'v'].indexOf(v) >= 0 ? v : 'o'
        }
    }
})