citty 0.1.6 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -15
- package/README.md +13 -42
- package/dist/THIRD-PARTY-LICENSES.md +33 -0
- package/dist/_chunks/libs/scule.mjs +65 -0
- package/dist/index.d.mts +77 -56
- package/dist/index.mjs +270 -436
- package/package.json +28 -35
- package/dist/index.cjs +0 -475
- package/dist/index.d.cts +0 -80
- package/dist/index.d.ts +0 -80
package/LICENSE
CHANGED
|
@@ -19,18 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
Parser is based on https://github.com/lukeed/mri
|
|
26
|
-
|
|
27
|
-
The MIT License (MIT)
|
|
28
|
-
|
|
29
|
-
Copyright (c) Luke Edwards luke.edwards05@gmail.com (lukeed.com)
|
|
30
|
-
|
|
31
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
32
|
-
|
|
33
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
34
|
-
|
|
35
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
36
|
-
|
package/README.md
CHANGED
|
@@ -1,45 +1,35 @@
|
|
|
1
1
|
# 🌆 citty
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
-
[![bundle][bundle-src]][bundle-href]
|
|
6
|
-
[![Codecov][codecov-src]][codecov-href]
|
|
7
|
-
[![License][license-src]][license-href]
|
|
3
|
+
<!-- automd:badges color=yellow bundlephobia -->
|
|
8
4
|
|
|
9
|
-
|
|
5
|
+
[](https://npmjs.com/package/citty)
|
|
6
|
+
[](https://npmjs.com/package/citty)
|
|
7
|
+
[](https://bundlephobia.com/package/citty)
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
<!-- /automd -->
|
|
10
|
+
|
|
11
|
+
Elegant CLI Builder
|
|
12
|
+
|
|
13
|
+
- Zero dependency
|
|
14
|
+
- Fast and lightweight argument parser (based on native [Node.js `util.parseArgs`](https://nodejs.org/api/util.html#utilparseargsconfig))
|
|
15
|
+
- Smart value parsing with typecast and boolean shortcuts
|
|
13
16
|
- Nested sub-commands
|
|
14
17
|
- Lazy and Async commands
|
|
15
|
-
-
|
|
18
|
+
- Pluggable and composable API
|
|
16
19
|
- Auto generated usage and help
|
|
17
20
|
|
|
18
|
-
🚧 This project is under heavy development. More features are coming soon!
|
|
19
|
-
|
|
20
21
|
## Usage
|
|
21
22
|
|
|
22
23
|
Install package:
|
|
23
24
|
|
|
24
25
|
```sh
|
|
25
|
-
|
|
26
|
-
npm install citty
|
|
27
|
-
|
|
28
|
-
# yarn
|
|
29
|
-
yarn add citty
|
|
30
|
-
|
|
31
|
-
# pnpm
|
|
32
|
-
pnpm install citty
|
|
26
|
+
npx nypm add -D citty
|
|
33
27
|
```
|
|
34
28
|
|
|
35
29
|
Import:
|
|
36
30
|
|
|
37
31
|
```js
|
|
38
|
-
// ESM
|
|
39
32
|
import { defineCommand, runMain } from "citty";
|
|
40
|
-
|
|
41
|
-
// CommonJS
|
|
42
|
-
const { defineCommand, runMain } = require("citty");
|
|
43
33
|
```
|
|
44
34
|
|
|
45
35
|
Define main command to run:
|
|
@@ -113,22 +103,3 @@ Renders usage and prints to the console
|
|
|
113
103
|
## License
|
|
114
104
|
|
|
115
105
|
Made with 💛 Published under [MIT License](./LICENSE).
|
|
116
|
-
|
|
117
|
-
Argument parser is based on [lukeed/mri](https://github.com/lukeed/mri) by Luke Edwards ([@lukeed](https://github.com/lukeed)).
|
|
118
|
-
|
|
119
|
-
<!-- Badges -->
|
|
120
|
-
|
|
121
|
-
<!-- Badges -->
|
|
122
|
-
|
|
123
|
-
[npm-version-src]: https://img.shields.io/npm/v/citty?style=flat&colorA=18181B&colorB=F0DB4F
|
|
124
|
-
[npm-version-href]: https://npmjs.com/package/citty
|
|
125
|
-
[npm-downloads-src]: https://img.shields.io/npm/dm/citty?style=flat&colorA=18181B&colorB=F0DB4F
|
|
126
|
-
[npm-downloads-href]: https://npmjs.com/package/citty
|
|
127
|
-
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/citty/main?style=flat&colorA=18181B&colorB=F0DB4F
|
|
128
|
-
[codecov-href]: https://codecov.io/gh/unjs/citty
|
|
129
|
-
[bundle-src]: https://img.shields.io/bundlephobia/minzip/citty?style=flat&colorA=18181B&colorB=F0DB4F
|
|
130
|
-
[bundle-href]: https://bundlephobia.com/result?p=citty
|
|
131
|
-
[license-src]: https://img.shields.io/github/license/unjs/citty.svg?style=flat&colorA=18181B&colorB=F0DB4F
|
|
132
|
-
[license-href]: https://github.com/unjs/citty/blob/main/LICENSE
|
|
133
|
-
[jsdocs-src]: https://img.shields.io/badge/jsDocs.io-reference-18181B?style=flat&colorA=18181B&colorB=F0DB4F
|
|
134
|
-
[jsdocs-href]: https://www.jsdocs.io/package/citty
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Licenses of Bundled Dependencies
|
|
2
|
+
|
|
3
|
+
The published artifact additionally contains code with the following licenses:
|
|
4
|
+
MIT
|
|
5
|
+
|
|
6
|
+
# Bundled Dependencies
|
|
7
|
+
|
|
8
|
+
## scule
|
|
9
|
+
|
|
10
|
+
License: MIT
|
|
11
|
+
Repository: https://github.com/unjs/scule
|
|
12
|
+
|
|
13
|
+
> MIT License
|
|
14
|
+
>
|
|
15
|
+
> Copyright (c) Pooya Parsa <pooya@pi0.io>
|
|
16
|
+
>
|
|
17
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
18
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
19
|
+
> in the Software without restriction, including without limitation the rights
|
|
20
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
21
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
22
|
+
> furnished to do so, subject to the following conditions:
|
|
23
|
+
>
|
|
24
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
25
|
+
> copies or substantial portions of the Software.
|
|
26
|
+
>
|
|
27
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
28
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
29
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
30
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
31
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
32
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
33
|
+
> SOFTWARE.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const NUMBER_CHAR_RE = /\d/;
|
|
2
|
+
const STR_SPLITTERS = [
|
|
3
|
+
"-",
|
|
4
|
+
"_",
|
|
5
|
+
"/",
|
|
6
|
+
"."
|
|
7
|
+
];
|
|
8
|
+
function isUppercase(char = "") {
|
|
9
|
+
if (NUMBER_CHAR_RE.test(char)) return;
|
|
10
|
+
return char !== char.toLowerCase();
|
|
11
|
+
}
|
|
12
|
+
function splitByCase(str, separators) {
|
|
13
|
+
const splitters = separators ?? STR_SPLITTERS;
|
|
14
|
+
const parts = [];
|
|
15
|
+
if (!str || typeof str !== "string") return parts;
|
|
16
|
+
let buff = "";
|
|
17
|
+
let previousUpper;
|
|
18
|
+
let previousSplitter;
|
|
19
|
+
for (const char of str) {
|
|
20
|
+
const isSplitter = splitters.includes(char);
|
|
21
|
+
if (isSplitter === true) {
|
|
22
|
+
parts.push(buff);
|
|
23
|
+
buff = "";
|
|
24
|
+
previousUpper = void 0;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const isUpper = isUppercase(char);
|
|
28
|
+
if (previousSplitter === false) {
|
|
29
|
+
if (previousUpper === false && isUpper === true) {
|
|
30
|
+
parts.push(buff);
|
|
31
|
+
buff = char;
|
|
32
|
+
previousUpper = isUpper;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (previousUpper === true && isUpper === false && buff.length > 1) {
|
|
36
|
+
const lastChar = buff.at(-1);
|
|
37
|
+
parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
|
|
38
|
+
buff = lastChar + char;
|
|
39
|
+
previousUpper = isUpper;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
buff += char;
|
|
44
|
+
previousUpper = isUpper;
|
|
45
|
+
previousSplitter = isSplitter;
|
|
46
|
+
}
|
|
47
|
+
parts.push(buff);
|
|
48
|
+
return parts;
|
|
49
|
+
}
|
|
50
|
+
function upperFirst(str) {
|
|
51
|
+
return str ? str[0].toUpperCase() + str.slice(1) : "";
|
|
52
|
+
}
|
|
53
|
+
function lowerFirst(str) {
|
|
54
|
+
return str ? str[0].toLowerCase() + str.slice(1) : "";
|
|
55
|
+
}
|
|
56
|
+
function pascalCase(str, opts) {
|
|
57
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
|
|
58
|
+
}
|
|
59
|
+
function camelCase(str, opts) {
|
|
60
|
+
return lowerFirst(pascalCase(str || "", opts));
|
|
61
|
+
}
|
|
62
|
+
function kebabCase(str, joiner) {
|
|
63
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
|
|
64
|
+
}
|
|
65
|
+
export { kebabCase as n, camelCase as t };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,80 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type ArgType = "boolean" | "string" | "enum" | "positional" | undefined;
|
|
3
|
+
type _ArgDef<T extends ArgType, VT extends boolean | number | string> = {
|
|
4
|
+
type?: T;
|
|
5
|
+
description?: string;
|
|
6
|
+
valueHint?: string;
|
|
7
|
+
alias?: string | string[];
|
|
8
|
+
default?: VT;
|
|
9
|
+
required?: boolean;
|
|
10
|
+
options?: string[];
|
|
9
11
|
};
|
|
10
|
-
type BooleanArgDef = _ArgDef<"boolean", boolean
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
type
|
|
12
|
+
type BooleanArgDef = Omit<_ArgDef<"boolean", boolean>, "options"> & {
|
|
13
|
+
negativeDescription?: string;
|
|
14
|
+
};
|
|
15
|
+
type StringArgDef = Omit<_ArgDef<"string", string>, "options">;
|
|
16
|
+
type EnumArgDef = _ArgDef<"enum", string>;
|
|
17
|
+
type PositionalArgDef = Omit<_ArgDef<"positional", string>, "alias" | "options">;
|
|
18
|
+
type ArgDef = BooleanArgDef | StringArgDef | PositionalArgDef | EnumArgDef;
|
|
14
19
|
type ArgsDef = Record<string, ArgDef>;
|
|
15
20
|
type Arg = ArgDef & {
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
name: string;
|
|
22
|
+
alias: string[];
|
|
23
|
+
};
|
|
24
|
+
type ResolveParsedArgType<T extends ArgDef, VT> = T extends {
|
|
25
|
+
default?: any;
|
|
26
|
+
required?: boolean;
|
|
27
|
+
} ? T["default"] extends NonNullable<VT> ? VT : T["required"] extends true ? VT : VT | undefined : VT | undefined;
|
|
28
|
+
type ParsedPositionalArg<T extends ArgDef> = T extends {
|
|
29
|
+
type: "positional";
|
|
30
|
+
} ? ResolveParsedArgType<T, string> : never;
|
|
31
|
+
type ParsedStringArg<T extends ArgDef> = T extends {
|
|
32
|
+
type: "string";
|
|
33
|
+
} ? ResolveParsedArgType<T, string> : never;
|
|
34
|
+
type ParsedBooleanArg<T extends ArgDef> = T extends {
|
|
35
|
+
type: "boolean";
|
|
36
|
+
} ? ResolveParsedArgType<T, boolean> : never;
|
|
37
|
+
type ParsedEnumArg<T extends ArgDef> = T extends {
|
|
38
|
+
type: "enum";
|
|
39
|
+
options: infer U;
|
|
40
|
+
} ? U extends Array<any> ? ResolveParsedArgType<T, U[number]> : never : never;
|
|
41
|
+
type RawArgs = {
|
|
42
|
+
_: string[];
|
|
18
43
|
};
|
|
19
|
-
type
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}[keyof T], string> & Record<{
|
|
26
|
-
[K in keyof T]: T[K] extends {
|
|
27
|
-
type: "string";
|
|
28
|
-
} ? K : never;
|
|
29
|
-
}[keyof T], string> & Record<{
|
|
30
|
-
[K in keyof T]: T[K] extends {
|
|
31
|
-
type: "boolean";
|
|
32
|
-
} ? K : never;
|
|
33
|
-
}[keyof T], boolean> & Record<string, string | boolean | string[]>;
|
|
44
|
+
type ParsedArg<T extends ArgDef> = T["type"] extends "positional" ? ParsedPositionalArg<T> : T["type"] extends "boolean" ? ParsedBooleanArg<T> : T["type"] extends "string" ? ParsedStringArg<T> : T["type"] extends "enum" ? ParsedEnumArg<T> : never;
|
|
45
|
+
type ParsedArgs<T extends ArgsDef = ArgsDef> = RawArgs & { [K in keyof T]: ParsedArg<T[K]> } & { [K in keyof T as T[K] extends {
|
|
46
|
+
alias: string;
|
|
47
|
+
} ? T[K]["alias"] : never]: ParsedArg<T[K]> } & { [K in keyof T as T[K] extends {
|
|
48
|
+
alias: string[];
|
|
49
|
+
} ? T[K]["alias"][number] : never]: ParsedArg<T[K]> } & Record<string, string | number | boolean | string[]>;
|
|
34
50
|
interface CommandMeta {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
name?: string;
|
|
52
|
+
version?: string;
|
|
53
|
+
description?: string;
|
|
54
|
+
hidden?: boolean;
|
|
38
55
|
}
|
|
39
56
|
type SubCommandsDef = Record<string, Resolvable<CommandDef<any>>>;
|
|
40
57
|
type CommandDef<T extends ArgsDef = ArgsDef> = {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
58
|
+
meta?: Resolvable<CommandMeta>;
|
|
59
|
+
args?: Resolvable<T>;
|
|
60
|
+
subCommands?: Resolvable<SubCommandsDef>;
|
|
61
|
+
setup?: (context: CommandContext<T>) => any | Promise<any>;
|
|
62
|
+
cleanup?: (context: CommandContext<T>) => any | Promise<any>;
|
|
63
|
+
run?: (context: CommandContext<T>) => any | Promise<any>;
|
|
47
64
|
};
|
|
48
65
|
type CommandContext<T extends ArgsDef = ArgsDef> = {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
66
|
+
rawArgs: string[];
|
|
67
|
+
args: ParsedArgs<T>;
|
|
68
|
+
cmd: CommandDef<T>;
|
|
69
|
+
subCommand?: CommandDef<T>;
|
|
70
|
+
data?: any;
|
|
54
71
|
};
|
|
55
72
|
type Awaitable<T> = () => T | Promise<T>;
|
|
56
73
|
type Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>);
|
|
57
|
-
|
|
58
|
-
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/command.d.ts
|
|
76
|
+
declare function defineCommand<const T extends ArgsDef = ArgsDef>(def: CommandDef<T>): CommandDef<T>;
|
|
59
77
|
interface RunCommandOptions {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
78
|
+
rawArgs: string[];
|
|
79
|
+
data?: any;
|
|
80
|
+
showUsage?: boolean;
|
|
63
81
|
}
|
|
64
82
|
declare function runCommand<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts: RunCommandOptions): Promise<{
|
|
65
|
-
|
|
83
|
+
result: unknown;
|
|
66
84
|
}>;
|
|
67
|
-
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/usage.d.ts
|
|
68
87
|
declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
|
|
69
88
|
declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
|
|
70
|
-
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/main.d.ts
|
|
71
91
|
interface RunMainOptions {
|
|
72
|
-
|
|
73
|
-
|
|
92
|
+
rawArgs?: string[];
|
|
93
|
+
showUsage?: typeof showUsage;
|
|
74
94
|
}
|
|
75
95
|
declare function runMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts?: RunMainOptions): Promise<void>;
|
|
76
96
|
declare function createMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>): (opts?: RunMainOptions) => Promise<void>;
|
|
77
|
-
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/args.d.ts
|
|
78
99
|
declare function parseArgs<T extends ArgsDef = ArgsDef>(rawArgs: string[], argsDef: ArgsDef): ParsedArgs<T>;
|
|
79
|
-
|
|
80
|
-
export {
|
|
100
|
+
//#endregion
|
|
101
|
+
export { Arg, ArgDef, ArgType, ArgsDef, Awaitable, BooleanArgDef, CommandContext, CommandDef, CommandMeta, EnumArgDef, ParsedArgs, PositionalArgDef, Resolvable, type RunCommandOptions, type RunMainOptions, StringArgDef, SubCommandsDef, _ArgDef, createMain, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
|