Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/masx200/mini-cli-args-parser
迷你版命令行参数解析器
https://github.com/masx200/mini-cli-args-parser
Last synced: 6 days ago
JSON representation
迷你版命令行参数解析器
- Host: GitHub
- URL: https://github.com/masx200/mini-cli-args-parser
- Owner: masx200
- License: mit
- Created: 2020-10-01T08:29:09.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-05T20:34:57.000Z (9 days ago)
- Last Synced: 2024-11-06T07:48:28.360Z (9 days ago)
- Language: TypeScript
- Size: 342 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mini-cli-args-parser
迷你版命令行参数解析器
Take a set of command line arguments, optionally with a set of options, and return an object representing the flags found in the passed arguments.
By default, any arguments starting with `-` or `--` are considered boolean flags. If the argument name is followed by an equal sign (`=`) it is considered a key-value pair. Any arguments which could not be parsed are available in `Array.from(parsedArgs)`.
Any arguments after '`--`' will not be parsed and will end up in `Array.from(parsedArgs)`.
# 安装
```shell
yarn add "@masx200/mini-cli-args-parser"
```# 使用方法
```js
import parse from "@masx200/mini-cli-args-parser";
const args = process.argv.slice(2);
const opts = parse(args);
const arr = Array.from(opts);console.log(opts);
console.log(arr);
```# 类型声明
```ts
function parse(args: string[]): any;
```## 获取命名参数
```js
const opts = parse(args);
```## 获取其他参数
```js
const arr = Array.from(opts);
```# 查看例子
```js
const args = [
"aaaaaaa",
"bbbbbb",
"--cccccc=555555555",
"--ddddd=xcvsaz",
"xxxxxxxxxxxxx",
"--ttt=bbb=1",
];const opts = parse(args);
const arr = Array.from(opts);console.log(opts);
console.log(arr);
``````
[Object: null prototype] {
cccccc: '555555555',
ddddd: 'xcvsaz',
ttt: 'bbb=1',
[Symbol(Symbol.iterator)]: [Function: bound values]
}
[ 'aaaaaaa', 'bbbbbb', 'xxxxxxxxxxxxx' ]```
```ts
const res = parse([
"-a",
"-he",
"--dddd",
"--eeeeee=3333",
"--",
"haha",
"--",
"-o",
"--p",
]);
const arr = [...res];
console.log(res, arr);
``````
[Object: null prototype] {
eeeeee: '3333',
a: true,
h: true,
e: true,
dddd: true,
[Symbol(Symbol.iterator)]: [Function (anonymous)]
} [ 'haha', '--', '-o', '--p' ]
```