erii 2.0.6 → 3.0.0-beta.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/readme.md CHANGED
@@ -1,152 +1,167 @@
1
- # Erii
2
-
3
- ![](./logo.png)
4
-
5
- [![npm version](https://badge.fury.io/js/erii.svg)](https://badge.fury.io/js/erii)
6
-
7
- ## Installation
8
- `npm install erii --save`
9
- ## Usage
10
-
11
- ```JavaScript
12
- const Erii = require('erii').default;
13
-
14
- Erii.setMetaInfo({
15
- version: '0.0.1',
16
- name: 'example'
17
- });
18
-
19
- // Bind commands
20
- Erii.bind({
21
- name: ['help', 'h'], // `h` will be set as an alias
22
- description: 'Show Help', // command description
23
- argument: {
24
- name: 'command',
25
- description: 'query help of a specified command'
26
- }
27
- }, (ctx, options) => {
28
- ctx.showHelp(); // show help text
29
- });
30
-
31
- // add options for `help` command
32
- Erii.addOption({
33
- name: ['verbose', 'debug'],
34
- command: 'help', // bind to command
35
- description: 'debug output', // option description
36
- argument: { // definition of option argument
37
- name: 'level',
38
- description: 'level of debug output'
39
- }
40
- });
41
-
42
- Erii.addOption({
43
- name: ['test'],
44
- // without binding to a specified command,
45
- // this option will be set as a common option.
46
- description: 'show test information',
47
- argument: {
48
- name: 'test-argument',
49
- description: 'test argument'
50
- }
51
- });
52
-
53
- Erii.start(); // don't forget to start Erii.
54
- ```
55
-
56
- **Example**
57
-
58
- Call with
59
-
60
- `node index.js --help xxx --debug 1`
61
-
62
- ```Javascript
63
- // ...
64
- // PART OF CODE
65
- Erii.bind({
66
- name: ['help', 'h'],
67
- description: 'Show Help',
68
- argument: {
69
- name: 'command',
70
- description: 'query help of a specified command'
71
- }
72
- }, (ctx, options) => {
73
- const { debug } = options;
74
- console.log(debug); // '1'
75
- console.log(ctx.getArgument()); // 'xxx'
76
- });
77
-
78
- Erii.addOption({
79
- name: ['verbose', 'debug'],
80
- description: 'show verbose output',
81
- argument: {
82
- name: 'level',
83
- description: 'level of verbose output'
84
- }
85
- });
86
-
87
- Erii.start();
88
- ```
89
-
90
-
91
- **Help Text**
92
- ```
93
- example / 0.0.1
94
-
95
- Help:
96
- Commands Description Alias
97
-
98
- --help <command> Show Help --h
99
- <command> query help of a specified comm
100
-
101
- Options:
102
-
103
- Options Description
104
- --verbose, debug <level> show verbose output
105
- <level> level of verbose output
106
- ```
107
-
108
- **Argument Validation**
109
-
110
- Argument validation are based on [validator.js](https://github.com/chriso/validator.js/).
111
-
112
- `Erii.validator` points to a `validator` exported by `validator.js`.
113
-
114
- Erii can validate arguments automatically.
115
-
116
- Define the validate methods in `argument` parameter.
117
-
118
- ```JavaScript
119
- Erii.addOption({
120
- name: ['verbose', 'debug'],
121
- description: 'show verbose output',
122
- argument: {
123
- name: 'level',
124
- description: 'level of verbose output',
125
- validate: 'isInt'
126
- }
127
- });
128
- ```
129
-
130
- `validate` can also be a function, for example:
131
-
132
- ```JavaScript
133
- Erii.addOption({
134
- name: ['verbose', 'debug'],
135
- description: 'show verbose output',
136
- argument: {
137
- name: 'level',
138
- description: 'level of verbose output',
139
- validate: (value) => Erii.validator.isInt(value)
140
- }
141
- });
142
- ```
143
-
144
- `argument.validate` works in both command and option definitions.
145
-
146
- **Example Output for Argument Validation**
147
-
148
- ```
149
- PS D:\Git\erii.test> node index.js --help --verbose f
150
- Argument validation failed for option 'verbose'.
151
- <level> should be a/an Int.
152
- ```
1
+ # Erii
2
+
3
+ ![](./logo.png)
4
+
5
+ [![npm version](https://badge.fury.io/js/erii.svg)](https://badge.fury.io/js/erii)
6
+
7
+ ## Installation
8
+ `npm install erii --save`
9
+ This package is ESM-only and requires Node.js 22.18 or later. Use `import` from an ESM project (`"type": "module"` in package.json, or an `.mjs` file).
10
+
11
+ ## Development
12
+
13
+ - `npm run build`: bundle ESM and TypeScript declarations with tsdown.
14
+ - `npm run typecheck`: check source and consumer types in strict mode.
15
+ - `npm test`: build, check types, and run regression tests.
16
+ - `npm run test:coverage`: run the same checks with coverage gates (100% lines/functions, 95% branches).
17
+
18
+ See [test/README.md](test/README.md) for the test scenario matrix. Runtime tests exercise the built ESM package; utility tests also cover source functions that are removed from the bundle when unused.
19
+
20
+ ## TypeScript
21
+
22
+ The package exports `Command`, `Option`, `Argument`, `CommandCtx`, `CommandHandler`, `CommandOptions`, `ArgumentValue`, `ArgumentValidator`, `ValidatorName`, `ParsedArguments`, and `MetaInfo` types. Handler options are always provided; individual options and `getArgument()` can be `undefined`. Values retain the parser's strings, numbers, booleans, arrays, and nested objects, so narrow them before use. String validators accept only validator.js methods callable with one string and returning a boolean.
23
+
24
+ ## Usage
25
+
26
+ ```JavaScript
27
+ import Erii from 'erii';
28
+
29
+ Erii.setMetaInfo({
30
+ version: '0.0.1',
31
+ name: 'example'
32
+ });
33
+
34
+ // Bind commands
35
+ Erii.bind({
36
+ name: ['help', 'h'], // `h` will be set as an alias
37
+ description: 'Show Help', // command description
38
+ argument: {
39
+ name: 'command',
40
+ description: 'query help of a specified command'
41
+ }
42
+ }, (ctx, options) => {
43
+ ctx.showHelp(); // show help text
44
+ });
45
+
46
+ // add options for `help` command
47
+ Erii.addOption({
48
+ name: ['verbose', 'debug'],
49
+ command: 'help', // bind to command
50
+ description: 'debug output', // option description
51
+ argument: { // definition of option argument
52
+ name: 'level',
53
+ description: 'level of debug output'
54
+ }
55
+ });
56
+
57
+ Erii.addOption({
58
+ name: ['test'],
59
+ // without binding to a specified command,
60
+ // this option will be set as a common option.
61
+ description: 'show test information',
62
+ argument: {
63
+ name: 'test-argument',
64
+ description: 'test argument'
65
+ }
66
+ });
67
+
68
+ Erii.start(); // don't forget to start Erii.
69
+ ```
70
+
71
+ **Example**
72
+
73
+ Call with
74
+
75
+ `node index.js --help xxx --debug 1`
76
+
77
+ ```Javascript
78
+ // ...
79
+ // PART OF CODE
80
+ Erii.bind({
81
+ name: ['help', 'h'],
82
+ description: 'Show Help',
83
+ argument: {
84
+ name: 'command',
85
+ description: 'query help of a specified command'
86
+ }
87
+ }, (ctx, options) => {
88
+ const { verbose } = options; // option aliases use the primary name
89
+ console.log(verbose); // 1
90
+ console.log(ctx.getArgument()); // 'xxx'
91
+ });
92
+
93
+ Erii.addOption({
94
+ name: ['verbose', 'debug'],
95
+ description: 'show verbose output',
96
+ argument: {
97
+ name: 'level',
98
+ description: 'level of verbose output'
99
+ }
100
+ });
101
+
102
+ Erii.start();
103
+ ```
104
+
105
+
106
+ **Help Text**
107
+ ```
108
+ example / 0.0.1
109
+
110
+ Help:
111
+ Commands Description Alias
112
+
113
+ --help <command> Show Help --h
114
+ <command> query help of a specified comm
115
+
116
+ Options:
117
+
118
+ Options Description
119
+ --verbose, debug <level> show verbose output
120
+ <level> level of verbose output
121
+ ```
122
+
123
+ **Argument Validation**
124
+
125
+ Argument validation are based on [validator.js](https://github.com/chriso/validator.js/).
126
+
127
+ `Erii.validator` points to a `validator` exported by `validator.js`.
128
+
129
+ Erii can validate arguments automatically.
130
+
131
+ Define the validate methods in `argument` parameter.
132
+
133
+ ```JavaScript
134
+ Erii.addOption({
135
+ name: ['verbose', 'debug'],
136
+ description: 'show verbose output',
137
+ argument: {
138
+ name: 'level',
139
+ description: 'level of verbose output',
140
+ validate: 'isInt'
141
+ }
142
+ });
143
+ ```
144
+
145
+ `validate` can also be a function, for example:
146
+
147
+ ```JavaScript
148
+ Erii.addOption({
149
+ name: ['verbose', 'debug'],
150
+ description: 'show verbose output',
151
+ argument: {
152
+ name: 'level',
153
+ description: 'level of verbose output',
154
+ validate: (value) => Erii.validator.isInt(String(value))
155
+ }
156
+ });
157
+ ```
158
+
159
+ `argument.validate` works in both command and option definitions.
160
+
161
+ **Example Output for Argument Validation**
162
+
163
+ ```
164
+ PS D:\Git\erii.test> node index.js --help --verbose f
165
+ Argument validation failed for option 'verbose'.
166
+ <level> should be a/an Int.
167
+ ```
package/dist/index.d.ts DELETED
@@ -1,108 +0,0 @@
1
- export interface Command {
2
- name: string | string[];
3
- description?: string;
4
- argument?: Argument;
5
- alias?: string[];
6
- redirect?: string;
7
- options?: Option[];
8
- handler?: (ctx: CommandCtx, options?: object) => any;
9
- }
10
- export interface Option {
11
- name: string | string[];
12
- description?: string;
13
- command?: string;
14
- argument?: Argument;
15
- }
16
- export interface CommandMap {
17
- [key: string]: Command;
18
- }
19
- export interface CommandCtx {
20
- showVersion: () => void;
21
- showHelp: () => void;
22
- getArgument: (commandName?: string) => string;
23
- }
24
- export interface Argument {
25
- name: string;
26
- description: string;
27
- validate?: string | ((value: any, logger: (message: string) => void) => boolean);
28
- }
29
- export declare class Erii {
30
- rawArguments: string[];
31
- parsedArguments: {
32
- _?: string[];
33
- [key: string]: string | string[];
34
- };
35
- private version;
36
- private name;
37
- commands: CommandMap;
38
- commonOptions: Option[];
39
- validator: any;
40
- alwaysHandler: () => void;
41
- defaultHandler: () => void;
42
- constructor();
43
- /**
44
- * 绑定命令处理函数
45
- * @param config
46
- * @param handler
47
- */
48
- bind(config: Command, handler: (ctx: CommandCtx, ...extraArguments: any[]) => any): void;
49
- /**
50
- * 总是执行
51
- * @param handler
52
- */
53
- always(handler: () => any): void;
54
- /**
55
- *
56
- */
57
- default(handler: () => any): void;
58
- /**
59
- * 增加设置项
60
- * @param config
61
- */
62
- addOption(config: Option): void;
63
- /**
64
- *
65
- * @param command
66
- */
67
- private commandCtx;
68
- /**
69
- * 设定基础信息
70
- * @param metaInfo
71
- */
72
- setMetaInfo({ version, name }?: {
73
- version?: string;
74
- name?: string;
75
- }): void;
76
- /**
77
- * 显示帮助信息
78
- */
79
- showHelp(command?: string): void;
80
- /**
81
- * 显示版本号
82
- */
83
- showVersion(): void;
84
- /**
85
- * 启动
86
- */
87
- start(): void;
88
- /**
89
- * 执行命令担当函数
90
- * @param command
91
- * @param extraArguments
92
- */
93
- private exec;
94
- validateArgument(argumentValue: any, argument: Argument): any;
95
- /**
96
- * 获得命令的参数
97
- * @param commandName
98
- * @param followRedirect 是否遵循重定向
99
- */
100
- getArgument(commandName: string, followRedirect?: boolean): string;
101
- /**
102
- * 启动
103
- * エリイ 起きてます❤
104
- */
105
- okite(): void;
106
- }
107
- declare const _default: Erii;
108
- export default _default;