yargs-parser
约 345 字大约 1 分钟
2024-07-29
NodeJS Deno Browser
概述
yargs-parser 是一个轻量级、高性能的 Node.js 命令行参数解析库,专注于将原始命令行输入(如 process.argv)转换为结构化的 JavaScript 对象。作为流行框架 yargs 的核心解析引擎,它提供了高度可定制的解析能力,适合直接集成或作为底层工具使用。
特性
参数结构化解析
将命令行输入(如
--name=foo -abc 100)解析为键值对对象,支持:- 布尔标志:
-a解析为{ a: true } - 键值参数:
--file config.txt解析为{ file: "config.txt" } - 数组与嵌套对象:
--file a.txt --file b.txt生成数组{ file: ["a.txt", "b.txt"] };--db.host localhost生成嵌套对象 {db: { host: "localhost" }} - 位置参数:未标记的参数保存在
_属性中(如node app.js run→{ _: ["run"] })
- 布尔标志:
解析配置
- 类型自动转换:启用
parse-numbers后,--port 8080自动转为数字(而非字符串) - 别名系统:
alias: { n: "name" }使-n foo等效于--name foo - 默认值:
default: { env: "dev" }确保未提供参数时使用默认值 - 驼峰转换:
camel-case-expansion配置将--my-option转为myOption
- 类型自动转换:启用
安装
npm
npm install yargs-parserpnpm
pnpm add yargs-parseryarn
yarn add yargs-parser使用
example.js
import parse from 'yargs-parser'
const argv = parse(process.argv.slice(2))
console.log(argv)node example.js --foo=33 --bar hello
{ _: [], foo: 33, bar: 'hello' }适用场景
- CLI 工具开发
