@breadc/core 1.0.0-beta.3 → 1.0.0-beta.4
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/index.d.mts +2 -0
- package/dist/index.mjs +41 -20
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -657,6 +657,7 @@ declare function printVersion(context: Context): string;
|
|
|
657
657
|
//#region src/error.d.ts
|
|
658
658
|
declare abstract class BreadcError extends Error {}
|
|
659
659
|
type BreadcAppErrorCause = {
|
|
660
|
+
group?: InternalGroup;
|
|
660
661
|
command?: InternalCommand;
|
|
661
662
|
commands?: (InternalCommand | InternalGroup)[];
|
|
662
663
|
};
|
|
@@ -665,6 +666,7 @@ type BreadcAppErrorInput = BreadcAppErrorCause & {
|
|
|
665
666
|
};
|
|
666
667
|
declare class BreadcAppError extends BreadcError {
|
|
667
668
|
static DUPLICATED_DEFAULT_COMMAND: string;
|
|
669
|
+
static DUPLICATED_DEFAULT_GROUP_COMMAND: string;
|
|
668
670
|
static DUPLICATED_GROUP: string;
|
|
669
671
|
static DUPLICATED_COMMAND: string;
|
|
670
672
|
static NO_ACTION_BOUND: string;
|
package/dist/index.mjs
CHANGED
|
@@ -38,6 +38,7 @@ function rawOption(spec, type, long, short, init) {
|
|
|
38
38
|
//#region src/error.ts
|
|
39
39
|
var BreadcError = class extends Error {};
|
|
40
40
|
var RuntimeError = class extends BreadcError {
|
|
41
|
+
static UNEXPECTED_ARGUMENTS = "Detect unexpected redundant arguments";
|
|
41
42
|
static REQUIRED_ARGUMENT_MISSING = "Missing required argument";
|
|
42
43
|
static OPTIONAL_ARGUMENT_ACCEPT_ONCE = "Optional argument can only be assigned once";
|
|
43
44
|
static REQUIRED_ARGUMENT_ACCEPT_ONCE = "Required argument can only be assigned once";
|
|
@@ -60,6 +61,7 @@ var RuntimeError = class extends BreadcError {
|
|
|
60
61
|
};
|
|
61
62
|
var BreadcAppError = class extends BreadcError {
|
|
62
63
|
static DUPLICATED_DEFAULT_COMMAND = `Find duplicated default commands`;
|
|
64
|
+
static DUPLICATED_DEFAULT_GROUP_COMMAND = `Find duplicated default group commands`;
|
|
63
65
|
static DUPLICATED_GROUP = `Find duplicated groups`;
|
|
64
66
|
static DUPLICATED_COMMAND = `Find duplicated commands`;
|
|
65
67
|
static NO_ACTION_BOUND = `There is no action function bound in this command`;
|
|
@@ -117,12 +119,10 @@ var ResolveOptionError = class extends BreadcError {
|
|
|
117
119
|
function resolveOptionInput(spec, description, init) {
|
|
118
120
|
return typeof spec === "string" ? option(spec, description, init) : spec;
|
|
119
121
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
125
|
-
}
|
|
122
|
+
const defaultUnknownOptionMiddleware = (_ctx, key, value) => ({
|
|
123
|
+
name: key,
|
|
124
|
+
value
|
|
125
|
+
});
|
|
126
126
|
|
|
127
127
|
//#endregion
|
|
128
128
|
//#region src/breadc/command.ts
|
|
@@ -157,7 +157,7 @@ function command(spec, init) {
|
|
|
157
157
|
run.allowUnknownOption = (middleware) => {
|
|
158
158
|
if (!run._unknownOptionMiddlewares) run._unknownOptionMiddlewares = [];
|
|
159
159
|
if (typeof middleware === "function") run._unknownOptionMiddlewares.push(middleware);
|
|
160
|
-
else run._unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware
|
|
160
|
+
else run._unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware);
|
|
161
161
|
return run;
|
|
162
162
|
};
|
|
163
163
|
run.action = (fn) => {
|
|
@@ -970,10 +970,14 @@ function parse(app, argv) {
|
|
|
970
970
|
commands: defaultCommands
|
|
971
971
|
});
|
|
972
972
|
const defaultCommand = defaultCommands[0];
|
|
973
|
-
doParse(context$1, defaultCommand !== void 0 && context$1.breadc._commands.length === 1 ? defaultCommand : void 0);
|
|
974
|
-
if (context$1.command || isVersion(context$1) || isHelp(context$1)) {} else if (
|
|
973
|
+
doParse(context$1, void 0, defaultCommand !== void 0 && context$1.breadc._commands.length === 1 ? defaultCommand : void 0);
|
|
974
|
+
if (context$1.command || isVersion(context$1) || isHelp(context$1)) {} else if (context$1.group) {
|
|
975
|
+
const matchedGroup = context$1.group;
|
|
976
|
+
reset(context$1);
|
|
977
|
+
doParse(context$1, matchedGroup, void 0);
|
|
978
|
+
} else if (defaultCommand) {
|
|
975
979
|
reset(context$1);
|
|
976
|
-
doParse(context$1, defaultCommand);
|
|
980
|
+
doParse(context$1, void 0, defaultCommand);
|
|
977
981
|
}
|
|
978
982
|
return context$1;
|
|
979
983
|
}
|
|
@@ -993,7 +997,7 @@ function isVersion(context) {
|
|
|
993
997
|
if (version && context.options.get(version.long)?.value()) return true;
|
|
994
998
|
else return false;
|
|
995
999
|
}
|
|
996
|
-
function doParse(context, defaultCommand) {
|
|
1000
|
+
function doParse(context, defaultGroup, defaultCommand) {
|
|
997
1001
|
const { breadc, tokens, options: matchedOptions } = context;
|
|
998
1002
|
let index = 0;
|
|
999
1003
|
let matchedGroup = void 0;
|
|
@@ -1030,10 +1034,8 @@ function doParse(context, defaultCommand) {
|
|
|
1030
1034
|
}
|
|
1031
1035
|
if (defaultCommand) {
|
|
1032
1036
|
matchedCommand = defaultCommand;
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
addPendingOptions(command._options);
|
|
1036
|
-
}
|
|
1037
|
+
buildCommand(defaultCommand);
|
|
1038
|
+
addPendingOptions(defaultCommand._options);
|
|
1037
1039
|
} else for (const command of breadc._commands) if (!command._default) for (let alias = 0; alias < command._pieces.length; alias++) addPendingCommand(command, alias);
|
|
1038
1040
|
while (!tokens.isEnd) {
|
|
1039
1041
|
const token = tokens.next();
|
|
@@ -1055,7 +1057,22 @@ function doParse(context, defaultCommand) {
|
|
|
1055
1057
|
});
|
|
1056
1058
|
buildGroup(group);
|
|
1057
1059
|
addPendingOptions(group._options);
|
|
1058
|
-
|
|
1060
|
+
if (matchedGroup && matchedGroup === defaultGroup) {
|
|
1061
|
+
const defaultCommands = matchedGroup._commands.filter((command) => command._pieces.some((p) => p.length === index));
|
|
1062
|
+
if (defaultCommands.length === 1) {
|
|
1063
|
+
const defaultCommand = defaultCommands[0];
|
|
1064
|
+
matchedCommand = defaultCommand;
|
|
1065
|
+
buildCommand(defaultCommand);
|
|
1066
|
+
addPendingOptions(defaultCommand._options);
|
|
1067
|
+
} else if (defaultCommands.length > 1) throw new BreadcAppError(BreadcAppError.DUPLICATED_DEFAULT_GROUP_COMMAND, {
|
|
1068
|
+
context,
|
|
1069
|
+
group: matchedGroup,
|
|
1070
|
+
commands: defaultCommands
|
|
1071
|
+
});
|
|
1072
|
+
} else for (const command of group._commands) for (let alias = 0; alias < command._pieces.length; alias++) {
|
|
1073
|
+
if (command._pieces[alias].length === index) continue;
|
|
1074
|
+
addPendingCommand(command, alias);
|
|
1075
|
+
}
|
|
1059
1076
|
} else {
|
|
1060
1077
|
if (!matchedCommand || matchedCommand === command) matchedCommand = command;
|
|
1061
1078
|
else throw new BreadcAppError(BreadcAppError.DUPLICATED_COMMAND, {
|
|
@@ -1095,6 +1112,7 @@ function doParse(context, defaultCommand) {
|
|
|
1095
1112
|
context.group = matchedGroup;
|
|
1096
1113
|
context.command = matchedCommand;
|
|
1097
1114
|
if (matchedCommand) {
|
|
1115
|
+
if (unknown.length > 0) throw new RuntimeError(RuntimeError.UNEXPECTED_ARGUMENTS, { context });
|
|
1098
1116
|
let i = 0;
|
|
1099
1117
|
for (; i < matchedCommand._arguments.length; i++) {
|
|
1100
1118
|
const argument = matchedCommand._arguments[i];
|
|
@@ -1192,8 +1210,11 @@ function group(spec, init) {
|
|
|
1192
1210
|
options.push(resolveOptionInput(spec, description, init));
|
|
1193
1211
|
return group;
|
|
1194
1212
|
},
|
|
1195
|
-
command(spec, init) {
|
|
1196
|
-
const command$2 = typeof spec === "string" ? command(spec, init
|
|
1213
|
+
command(spec, description, init) {
|
|
1214
|
+
const command$2 = typeof spec === "string" ? command(spec, description || init ? {
|
|
1215
|
+
description,
|
|
1216
|
+
...init
|
|
1217
|
+
} : void 0) : spec;
|
|
1197
1218
|
command$2._group = group;
|
|
1198
1219
|
commands.push(command$2);
|
|
1199
1220
|
return command$2;
|
|
@@ -1204,7 +1225,7 @@ function group(spec, init) {
|
|
|
1204
1225
|
},
|
|
1205
1226
|
allowUnknownOption(middleware) {
|
|
1206
1227
|
if (typeof middleware === "function") unknownOptionMiddlewares.push(middleware);
|
|
1207
|
-
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware
|
|
1228
|
+
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware);
|
|
1208
1229
|
return group;
|
|
1209
1230
|
}
|
|
1210
1231
|
};
|
|
@@ -1255,7 +1276,7 @@ function breadc(name, init = {}) {
|
|
|
1255
1276
|
},
|
|
1256
1277
|
allowUnknownOption(middleware) {
|
|
1257
1278
|
if (typeof middleware === "function") unknownOptionMiddlewares.push(middleware);
|
|
1258
|
-
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware
|
|
1279
|
+
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware);
|
|
1259
1280
|
return app;
|
|
1260
1281
|
},
|
|
1261
1282
|
parse(argv) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breadc/core",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
4
|
"description": "Yet another Command Line Application Framework with fully strong TypeScript support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"breadc",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@breadc/color": "1.0.0-beta.
|
|
36
|
+
"@breadc/color": "1.0.0-beta.4"
|
|
37
37
|
},
|
|
38
38
|
"size-limit": [
|
|
39
39
|
{
|