citty 0.1.5 → 0.2.0

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 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
- [![npm version][npm-version-src]][npm-version-href]
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
- > Elegant CLI Builder
5
+ [![npm version](https://img.shields.io/npm/v/citty?color=yellow)](https://npmjs.com/package/citty)
6
+ [![npm downloads](https://img.shields.io/npm/dm/citty?color=yellow)](https://npmjs.com/package/citty)
7
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/citty?color=yellow)](https://bundlephobia.com/package/citty)
10
8
 
11
- - Fast and lightweight argument parser based on [mri](https://github.com/lukeed/mri)
12
- - Smart value parsing with typecast, boolean shortcuts and unknown flag handling
9
+ <!-- /automd -->
10
+
11
+ Elegant CLI Builder
12
+
13
+ - Zero dependency
14
+ - Fast and lightweight argument parser (based on native [node utils](arg parser](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
- - Plugable and composable API
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
- # npm
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,68 @@
1
+ //#region node_modules/.pnpm/scule@1.3.0/node_modules/scule/dist/index.mjs
2
+ const NUMBER_CHAR_RE = /\d/;
3
+ const STR_SPLITTERS = [
4
+ "-",
5
+ "_",
6
+ "/",
7
+ "."
8
+ ];
9
+ function isUppercase(char = "") {
10
+ if (NUMBER_CHAR_RE.test(char)) return;
11
+ return char !== char.toLowerCase();
12
+ }
13
+ function splitByCase(str, separators) {
14
+ const splitters = separators ?? STR_SPLITTERS;
15
+ const parts = [];
16
+ if (!str || typeof str !== "string") return parts;
17
+ let buff = "";
18
+ let previousUpper;
19
+ let previousSplitter;
20
+ for (const char of str) {
21
+ const isSplitter = splitters.includes(char);
22
+ if (isSplitter === true) {
23
+ parts.push(buff);
24
+ buff = "";
25
+ previousUpper = void 0;
26
+ continue;
27
+ }
28
+ const isUpper = isUppercase(char);
29
+ if (previousSplitter === false) {
30
+ if (previousUpper === false && isUpper === true) {
31
+ parts.push(buff);
32
+ buff = char;
33
+ previousUpper = isUpper;
34
+ continue;
35
+ }
36
+ if (previousUpper === true && isUpper === false && buff.length > 1) {
37
+ const lastChar = buff.at(-1);
38
+ parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
39
+ buff = lastChar + char;
40
+ previousUpper = isUpper;
41
+ continue;
42
+ }
43
+ }
44
+ buff += char;
45
+ previousUpper = isUpper;
46
+ previousSplitter = isSplitter;
47
+ }
48
+ parts.push(buff);
49
+ return parts;
50
+ }
51
+ function upperFirst(str) {
52
+ return str ? str[0].toUpperCase() + str.slice(1) : "";
53
+ }
54
+ function lowerFirst(str) {
55
+ return str ? str[0].toLowerCase() + str.slice(1) : "";
56
+ }
57
+ function pascalCase(str, opts) {
58
+ return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
59
+ }
60
+ function camelCase(str, opts) {
61
+ return lowerFirst(pascalCase(str || "", opts));
62
+ }
63
+ function kebabCase(str, joiner) {
64
+ return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
65
+ }
66
+
67
+ //#endregion
68
+ export { kebabCase as n, camelCase as t };
package/dist/index.d.mts CHANGED
@@ -1,80 +1,101 @@
1
- type ArgType = "boolean" | "string" | "positional" | undefined;
2
- type _ArgDef<T extends ArgType, VT extends boolean | string> = {
3
- type?: T;
4
- description?: string;
5
- valueHint?: string;
6
- alias?: string | string[];
7
- default?: VT;
8
- required?: boolean;
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
- type StringArgDef = _ArgDef<"string", string>;
12
- type PositionalArgDef = Omit<_ArgDef<"positional", string>, "alias">;
13
- type ArgDef = BooleanArgDef | StringArgDef | PositionalArgDef;
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
- name: string;
17
- alias: string[];
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 ParsedArgs<T extends ArgsDef = ArgsDef> = {
20
- _: string[];
21
- } & Record<{
22
- [K in keyof T]: T[K] extends {
23
- type: "positional";
24
- } ? K : never;
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
- name?: string;
36
- version?: string;
37
- description?: string;
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
- meta?: Resolvable<CommandMeta>;
42
- args?: Resolvable<T>;
43
- subCommands?: Resolvable<SubCommandsDef>;
44
- setup?: (context: CommandContext<T>) => any | Promise<any>;
45
- cleanup?: (context: CommandContext<T>) => any | Promise<any>;
46
- run?: (context: CommandContext<T>) => any | Promise<any>;
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
- rawArgs: string[];
50
- args: ParsedArgs<T>;
51
- cmd: CommandDef<T>;
52
- subCommand?: CommandDef<T>;
53
- data?: any;
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
- declare function defineCommand<T extends ArgsDef = ArgsDef>(def: CommandDef<T>): CommandDef<T>;
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
- rawArgs: string[];
61
- data?: any;
62
- showUsage?: boolean;
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
- result: unknown;
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
- rawArgs?: string[];
73
- showUsage?: typeof showUsage;
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 { type Arg, type ArgDef, type ArgType, type ArgsDef, type Awaitable, type BooleanArgDef, type CommandContext, type CommandDef, type CommandMeta, type ParsedArgs, type PositionalArgDef, type Resolvable, type RunCommandOptions, type RunMainOptions, type StringArgDef, type SubCommandsDef, type _ArgDef, createMain, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
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 };