Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@chewbank/argv-router

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chewbank/argv-router

简单的node.js命令行参数路由分发器

  • 1.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Install

npm install argv-router

匹配表达式

匹配表达式分为单参数和组合参数两种,不可混合使用

单参数

单参数时直接指定参数名即可,使用简写、全称命名时用英文逗号分隔

const argvRouter = require('argv-router')

argvRouter({
   '-v, --version'(argv) {
      console.log(argv)
   },
   '-w, --watch, <>'(argv) {
      console.log(argv)
   }
})
组合参数

组合参数时,多个参数使用空格分隔

const argvRouter = require('argv-router')

argvRouter({
   '-a -w'(argv) {
      console.log(argv)
   },
})

自动扩展

在使用组合参数时可以搭配单参数实现自动扩展,使用单参数中的任意简写或全称命名。如以下示例中组合参数“-a -w”会尝试匹配“-a -w”、“-a --watch”、“--async -w”、“--async --watch”。

let options = {
   '-w, --watch'(argv) {

   },
   '-a, --async'(argv) {

   },
   '-a -w'(argv) {

   }
}

argvRouter(options)

参数值匹配

通过“<>”占位符号定义是否匹配参数值

const argvRouter = require('argv-router')

argvRouter({
   '-a <> -w <>'(argv) {
      console.log(argv)
   },
})

快速赋值

通过“[$name]”定义是否启用无参数名的快捷赋值选项,在argv中返回一个以“$name”命名的匹配项数组

const argvRouter = require('argv-router')

argvRouter({
   '[files] -a <> -w <>'(argv) {
      console.log(argv.files)
   },
})

匹配优先级

首先按匹配参数数量进行升级,匹配参数越多优先级越高。在等量参数数量下,包含快速赋值的匹配项先级高于其它选项。

默认匹配

在参数为空时指定默认行为

let options = {
   '-w, --watch'(argv) {

   },
   '-a, --async'(argv) {

   }
}

let router = argvRouter(options, '-w')

动态执行

在js中动态执行指定的cli命令

let router = argvRouter(options)

router.execute('-v')

router.execute('-w -a')

注意事项

参数名必须使用“--”或“-”,否则视为语法错误。

FAQs

Package last updated on 18 Nov 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc