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

cli-param

v1.0.8

Published

**CLI-Param**是一个轻量的用于解析命令行参数以方便js程序读取的包。传入一个模板对象,用于定义解析参数的方式,然后**CLI-Param**会将命令行参数解析成一个js对象。如果用户输入的参数不符合模板标准,会自动打印错误信息,也可以自定义错误信息。

Downloads

18

Readme

CLI-Param

CLI-Param是一个轻量的用于解析命令行参数以方便js程序读取的包。传入一个模板对象,用于定义解析参数的方式,然后CLI-Param会将命令行参数解析成一个js对象。如果用户输入的参数不符合模板标准,会自动打印错误信息,也可以自定义错误信息。

快速入门

const cliParam = require('cli-param')
const template = { //模板对象
   handle(param) { //处理方法
      console.log(param); //打印解析完成的参数对象
   },
   bsd: [
      {
         name: 'age1',  //参数名
         describe: '参数描述1'
      },
      {
         name: 'age2', //参数名
         describe: '参数描述2'
      }
   ]
}
cliParam(template)
$> cli p1 p2
{ age1: 'p1', age2: 'p2' }

如果缺少一个bsd参数则会打印错误信息:

$> cli p1
缺少参数"age2"作为第"2"个参数
参数描述2

这些没有选项名的参数我们称为无名参数。

仅仅有这些还远远不够,很多时候需要能解析带选项的参数。

使用双减号--加选项名

const cliParam = require('cli-param')
const template = {
   handle(m) {
      console.log(m);
   },
   bsd: [
      {
         name: 'age1', //参数名
         describe: '参数描述1'
      }
   ],
   option: [
      {
         name: 'op1', //选项名
         ab: 'o' //选项简写
      },
      {
         name: 'op2', //选项名
         ab: 'a' //选项简写
      },
      {
         name: 'op3', //选项名
         ab: 'b' //选项简写
      }
   ]
}
cliParam(template)
$> cli p1 --op1 OP1
{ age1: 'p1', op1: 'OP1' }

也可以用选项的简写,将使用单个-,解析结果仍会使用其全称作为键名。

$> cli p1 -o OP1
{ age1: 'p1', op1: 'OP1' }

如果缺省参数的值,将会被赋予true,方便作为模式选项使用。

以下写法的解析结果是一致的

但是第一种写法后面可能会跟值,而第二种只能作为模式选项。

$> cli p1 -a -b
$> cli p1 -ab
{ age1: 'p1', op3: true, op2: true }

多命令

有时候需要让一个命令有多个子命令,就像$> npm install cli-param中的install就是一个子命令。在CLI-Param中,使用subcmd来创建子命令。

解析成功后会调用不同的handle函数来处理参数。

const cliParam = require('cli-param')
const template = {
   subcmd: {
      install: {
         describe: '下载',
         handle(m) {
            console.log('下载', m.package);
         },
         bsd: [
            {
               name: 'package',
               describe: '要下载的包',
               more: true //允许接受多个值
            }
         ]
      },
      uninstall: {
         describe: '卸载',
         handle(m) {
            console.log('卸载', m.package);
         },
         bsd: [
            {
               name: 'package',
               describe: '要卸载的包',
               more: true
            }
         ]
      }
   }
}
cliParam(template)
$> cli install p1 p2
下载 [ 'p1', 'p2' ]
$> cli uninstall p1 p2 
卸载 [ 'p1', 'p2' ]