bob-core 0.7.0 → 0.8.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/dist/Command.d.ts +1 -1
- package/dist/Command.js +1 -1
- package/dist/CommandHelper.js +11 -3
- package/dist/Parser.d.ts +3 -2
- package/dist/Parser.js +25 -8
- package/package.json +1 -1
package/dist/Command.d.ts
CHANGED
|
@@ -17,5 +17,5 @@ export declare abstract class Command<C = undefined> extends CommandHelper {
|
|
|
17
17
|
protected abstract handle(): Promise<void | number>;
|
|
18
18
|
run(ctx: C, ...args: any[]): Promise<number>;
|
|
19
19
|
protected option(key: string, defaultValue?: any): string | null;
|
|
20
|
-
protected argument(key: string, defaultValue?: any): string | null;
|
|
20
|
+
protected argument(key: string, defaultValue?: any): string | Array<string> | null;
|
|
21
21
|
}
|
package/dist/Command.js
CHANGED
|
@@ -26,7 +26,7 @@ class Command extends CommandHelper_1.CommandHelper {
|
|
|
26
26
|
option(key, defaultValue = null) {
|
|
27
27
|
return this.parser.option(key) ?? defaultValue;
|
|
28
28
|
}
|
|
29
|
-
argument(key, defaultValue) {
|
|
29
|
+
argument(key, defaultValue = null) {
|
|
30
30
|
return this.parser.argument(key) ?? defaultValue;
|
|
31
31
|
}
|
|
32
32
|
}
|
package/dist/CommandHelper.js
CHANGED
|
@@ -39,7 +39,15 @@ class CommandHelper {
|
|
|
39
39
|
log((0, chalk_1.default) `\n{yellow Arguments}:`);
|
|
40
40
|
for (const signature of availableArguments) {
|
|
41
41
|
const spaces = (0, string_1.generateSpace)(maxLength - signature.name.length);
|
|
42
|
-
|
|
42
|
+
let message = (0, chalk_1.default) ` {green ${signature.name}} ${spaces} ${signature.help ?? '\b'}`;
|
|
43
|
+
if (signature.defaultValue !== undefined && signature.optional) {
|
|
44
|
+
const defaultValue = signature.type === 'array' ? JSON.stringify(signature.defaultValue) : signature.defaultValue;
|
|
45
|
+
message += (0, chalk_1.default) ` {yellow [default: ${defaultValue}]}`;
|
|
46
|
+
}
|
|
47
|
+
if (signature.variadic) {
|
|
48
|
+
message += (0, chalk_1.default) ` {white (variadic)}`;
|
|
49
|
+
}
|
|
50
|
+
log(message);
|
|
43
51
|
}
|
|
44
52
|
}
|
|
45
53
|
if (availableOptions.length > 0) {
|
|
@@ -51,8 +59,8 @@ class CommandHelper {
|
|
|
51
59
|
message += (0, chalk_1.default) ` {white (${signature.type})}`;
|
|
52
60
|
}
|
|
53
61
|
if (signature.defaultValue !== undefined && signature.optional) {
|
|
54
|
-
const
|
|
55
|
-
message += (0, chalk_1.default) ` {yellow [default: ${
|
|
62
|
+
const defaultValue = signature.type === 'array' ? JSON.stringify(signature.defaultValue) : signature.defaultValue;
|
|
63
|
+
message += (0, chalk_1.default) ` {yellow [default: ${defaultValue}]}`;
|
|
56
64
|
}
|
|
57
65
|
log(message);
|
|
58
66
|
}
|
package/dist/Parser.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export type ArgSignature = {
|
|
2
2
|
name: string;
|
|
3
3
|
type: string;
|
|
4
|
-
optional
|
|
4
|
+
optional?: boolean;
|
|
5
|
+
variadic?: boolean;
|
|
5
6
|
alias?: string[];
|
|
6
7
|
help?: string;
|
|
7
|
-
defaultValue?: string | boolean | Array<
|
|
8
|
+
defaultValue?: string | boolean | Array<string> | null;
|
|
8
9
|
isOption?: boolean;
|
|
9
10
|
};
|
|
10
11
|
export declare class Parser {
|
package/dist/Parser.js
CHANGED
|
@@ -29,6 +29,9 @@ class Parser {
|
|
|
29
29
|
return Boolean(this.options[name]);
|
|
30
30
|
}
|
|
31
31
|
if (signature.type === 'array') {
|
|
32
|
+
if (!this.options[name]) {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
32
35
|
return Array.isArray(this.options[name]) ? this.options[name] : [this.options[name]];
|
|
33
36
|
}
|
|
34
37
|
return this.options[name];
|
|
@@ -65,8 +68,14 @@ class Parser {
|
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
else {
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
if (param.variadic) {
|
|
72
|
+
const paramValue = paramValues.splice(0, paramValues.length);
|
|
73
|
+
this.arguments[param.name] = paramValue ?? [];
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const paramValue = paramValues.shift();
|
|
77
|
+
this.arguments[param.name] = paramValue ?? param.defaultValue ?? null;
|
|
78
|
+
}
|
|
70
79
|
this.argumentsSignature[param.name] = param;
|
|
71
80
|
}
|
|
72
81
|
}
|
|
@@ -74,16 +83,22 @@ class Parser {
|
|
|
74
83
|
parseParamSignature(argument) {
|
|
75
84
|
let cleanedArgs = argument;
|
|
76
85
|
let isOptional = false;
|
|
77
|
-
|
|
86
|
+
let isVariadic = false;
|
|
87
|
+
if (cleanedArgs.endsWith('?')) {
|
|
78
88
|
cleanedArgs = cleanedArgs.slice(0, -1);
|
|
79
89
|
isOptional = true;
|
|
80
90
|
}
|
|
91
|
+
if (cleanedArgs.endsWith('*')) {
|
|
92
|
+
cleanedArgs = cleanedArgs.slice(0, -1);
|
|
93
|
+
isVariadic = true;
|
|
94
|
+
}
|
|
81
95
|
const arg = {
|
|
82
96
|
name: cleanedArgs,
|
|
83
97
|
optional: isOptional,
|
|
84
|
-
type: 'string',
|
|
98
|
+
type: isVariadic ? 'array' : 'string',
|
|
85
99
|
help: undefined,
|
|
86
|
-
defaultValue: null,
|
|
100
|
+
defaultValue: isVariadic ? [] : null,
|
|
101
|
+
variadic: isVariadic,
|
|
87
102
|
isOption: false
|
|
88
103
|
};
|
|
89
104
|
if (arg.name.includes(':')) {
|
|
@@ -110,6 +125,7 @@ class Parser {
|
|
|
110
125
|
}
|
|
111
126
|
else {
|
|
112
127
|
if (arg.name.startsWith('--')) {
|
|
128
|
+
arg.optional = true;
|
|
113
129
|
arg.defaultValue = false;
|
|
114
130
|
arg.type = 'boolean';
|
|
115
131
|
}
|
|
@@ -127,9 +143,7 @@ class Parser {
|
|
|
127
143
|
arg.defaultValue = [];
|
|
128
144
|
arg.type = 'array';
|
|
129
145
|
}
|
|
130
|
-
|
|
131
|
-
arg.help = this.helperDefinitions[arg.name];
|
|
132
|
-
}
|
|
146
|
+
arg.help = arg.help ?? this.helperDefinitions[arg.name] ?? this.helperDefinitions[`--${arg.name}`];
|
|
133
147
|
return arg;
|
|
134
148
|
}
|
|
135
149
|
validate() {
|
|
@@ -139,6 +153,9 @@ class Parser {
|
|
|
139
153
|
if (!value && !argSignature.optional) {
|
|
140
154
|
throw new MissingRequiredArgumentValue_1.MissingRequiredArgumentValue(argSignature);
|
|
141
155
|
}
|
|
156
|
+
if (!value?.length && argSignature.variadic && !argSignature.optional) {
|
|
157
|
+
throw new MissingRequiredArgumentValue_1.MissingRequiredArgumentValue(argSignature);
|
|
158
|
+
}
|
|
142
159
|
}
|
|
143
160
|
}
|
|
144
161
|
}
|