@rushstack/ts-command-line 4.21.5 → 4.22.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/dist/ts-command-line.d.ts +1 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/CommandLineHelper.js +2 -1
- package/lib/CommandLineHelper.js.map +1 -1
- package/lib/Constants.d.ts +1 -1
- package/lib/Constants.d.ts.map +1 -1
- package/lib/Constants.js +13 -1
- package/lib/Constants.js.map +1 -1
- package/lib/TypeUuidLite.d.ts +9 -0
- package/lib/TypeUuidLite.d.ts.map +1 -0
- package/lib/TypeUuidLite.js +32 -0
- package/lib/TypeUuidLite.js.map +1 -0
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/providers/CommandLineParser.d.ts.map +1 -1
- package/lib/providers/CommandLineParser.js +7 -0
- package/lib/providers/CommandLineParser.js.map +1 -1
- package/lib/providers/TabCompletionAction.js +2 -1
- package/lib/providers/TabCompletionAction.js.map +1 -1
- package/package.json +4 -4
|
@@ -129,7 +129,7 @@ export declare class CommandLineChoiceParameter<TChoice extends string = string>
|
|
|
129
129
|
*
|
|
130
130
|
* @public
|
|
131
131
|
*/
|
|
132
|
-
export declare
|
|
132
|
+
export declare enum CommandLineConstants {
|
|
133
133
|
/**
|
|
134
134
|
* The name of the built-in action that serves suggestions for tab-completion
|
|
135
135
|
*/
|
package/dist/tsdoc-metadata.json
CHANGED
package/lib/CommandLineHelper.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.CommandLineHelper = void 0;
|
|
6
|
+
const Constants_1 = require("./Constants");
|
|
6
7
|
/**
|
|
7
8
|
* Helpers for working with the ts-command-line API.
|
|
8
9
|
*
|
|
@@ -15,7 +16,7 @@ class CommandLineHelper {
|
|
|
15
16
|
* @public
|
|
16
17
|
*/
|
|
17
18
|
static isTabCompletionActionRequest(argv) {
|
|
18
|
-
return argv && argv.length > 2 && argv[2] ===
|
|
19
|
+
return argv && argv.length > 2 && argv[2] === Constants_1.CommandLineConstants.TabCompletionActionName;
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
exports.CommandLineHelper = CommandLineHelper;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommandLineHelper.js","sourceRoot":"","sources":["../src/CommandLineHelper.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;
|
|
1
|
+
{"version":3,"file":"CommandLineHelper.js","sourceRoot":"","sources":["../src/CommandLineHelper.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,2CAAmD;AAEnD;;;;GAIG;AACH,MAAa,iBAAiB;IAC5B;;;;OAIG;IACI,MAAM,CAAC,4BAA4B,CAAC,IAAc;QACvD,OAAO,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,gCAAoB,CAAC,uBAAuB,CAAC;IAC7F,CAAC;CACF;AATD,8CASC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { CommandLineConstants } from './Constants';\n\n/**\n * Helpers for working with the ts-command-line API.\n *\n * @public\n */\nexport class CommandLineHelper {\n /**\n * Returns true if the current command line action is tab-complete.\n *\n * @public\n */\n public static isTabCompletionActionRequest(argv: string[]): boolean {\n return argv && argv.length > 2 && argv[2] === CommandLineConstants.TabCompletionActionName;\n }\n}\n"]}
|
package/lib/Constants.d.ts
CHANGED
package/lib/Constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Constants.d.ts","sourceRoot":"","sources":["../src/Constants.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,
|
|
1
|
+
{"version":3,"file":"Constants.d.ts","sourceRoot":"","sources":["../src/Constants.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,oBAAY,oBAAoB;IAC9B;;OAEG;IACH,uBAAuB,iBAAiB;CACzC;AAED,eAAO,MAAM,uBAAuB,EAAE,OAAO,MAA0B,CAAC"}
|
package/lib/Constants.js
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.SCOPING_PARAMETER_GROUP = void 0;
|
|
5
|
+
exports.SCOPING_PARAMETER_GROUP = exports.CommandLineConstants = void 0;
|
|
6
|
+
/**
|
|
7
|
+
* String constants for command line processing.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
var CommandLineConstants;
|
|
12
|
+
(function (CommandLineConstants) {
|
|
13
|
+
/**
|
|
14
|
+
* The name of the built-in action that serves suggestions for tab-completion
|
|
15
|
+
*/
|
|
16
|
+
CommandLineConstants["TabCompletionActionName"] = "tab-complete";
|
|
17
|
+
})(CommandLineConstants || (exports.CommandLineConstants = CommandLineConstants = {}));
|
|
6
18
|
exports.SCOPING_PARAMETER_GROUP = Symbol('scoping');
|
|
7
19
|
//# sourceMappingURL=Constants.js.map
|
package/lib/Constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Constants.js","sourceRoot":"","sources":["../src/Constants.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;
|
|
1
|
+
{"version":3,"file":"Constants.js","sourceRoot":"","sources":["../src/Constants.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;GAIG;AACH,IAAY,oBAKX;AALD,WAAY,oBAAoB;IAC9B;;OAEG;IACH,gEAAwC,CAAA;AAC1C,CAAC,EALW,oBAAoB,oCAApB,oBAAoB,QAK/B;AAEY,QAAA,uBAAuB,GAAkB,MAAM,CAAC,SAAS,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * String constants for command line processing.\n *\n * @public\n */\nexport enum CommandLineConstants {\n /**\n * The name of the built-in action that serves suggestions for tab-completion\n */\n TabCompletionActionName = 'tab-complete'\n}\n\nexport const SCOPING_PARAMETER_GROUP: unique symbol = Symbol('scoping');\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const uuidAlreadyReportedError: string;
|
|
2
|
+
export declare class TypeUuid {
|
|
3
|
+
/**
|
|
4
|
+
* Returns true if the `targetObject` is an instance of a JavaScript class that was previously
|
|
5
|
+
* registered using the specified `typeUuid`. Base classes are also considered.
|
|
6
|
+
*/
|
|
7
|
+
static isInstanceOf(targetObject: unknown, typeUuid: string): boolean;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=TypeUuidLite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TypeUuidLite.d.ts","sourceRoot":"","sources":["../src/TypeUuidLite.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,wBAAwB,EAAE,MAA+C,CAAC;AAGvF,qBAAa,QAAQ;IACnB;;;OAGG;WACW,YAAY,CAAC,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;CAkB7E"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
|
+
// See LICENSE in the project root for license information.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.TypeUuid = exports.uuidAlreadyReportedError = void 0;
|
|
6
|
+
const classPrototypeUuidSymbol = Symbol.for('TypeUuid.classPrototypeUuid');
|
|
7
|
+
exports.uuidAlreadyReportedError = 'f26b0640-a49b-49d1-9ead-1a516d5920c7';
|
|
8
|
+
// Avoid a dependency on node-core-library to access just this one API:
|
|
9
|
+
class TypeUuid {
|
|
10
|
+
/**
|
|
11
|
+
* Returns true if the `targetObject` is an instance of a JavaScript class that was previously
|
|
12
|
+
* registered using the specified `typeUuid`. Base classes are also considered.
|
|
13
|
+
*/
|
|
14
|
+
static isInstanceOf(targetObject, typeUuid) {
|
|
15
|
+
if (targetObject === undefined || targetObject === null) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
let objectPrototype = Object.getPrototypeOf(targetObject);
|
|
19
|
+
while (objectPrototype !== undefined && objectPrototype !== null) {
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
+
const registeredUuid = objectPrototype[classPrototypeUuidSymbol];
|
|
22
|
+
if (registeredUuid === typeUuid) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
// Walk upwards an examine base class prototypes
|
|
26
|
+
objectPrototype = Object.getPrototypeOf(objectPrototype);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.TypeUuid = TypeUuid;
|
|
32
|
+
//# sourceMappingURL=TypeUuidLite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TypeUuidLite.js","sourceRoot":"","sources":["../src/TypeUuidLite.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,MAAM,wBAAwB,GAAW,MAAM,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;AAEtE,QAAA,wBAAwB,GAAW,sCAAsC,CAAC;AAEvF,uEAAuE;AACvE,MAAa,QAAQ;IACnB;;;OAGG;IACI,MAAM,CAAC,YAAY,CAAC,YAAqB,EAAE,QAAgB;QAChE,IAAI,YAAY,KAAK,SAAS,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;YACxD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,eAAe,GAAO,MAAM,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;QAC9D,OAAO,eAAe,KAAK,SAAS,IAAI,eAAe,KAAK,IAAI,EAAE,CAAC;YACjE,8DAA8D;YAC9D,MAAM,cAAc,GAAY,eAAuB,CAAC,wBAAwB,CAAC,CAAC;YAClF,IAAI,cAAc,KAAK,QAAQ,EAAE,CAAC;gBAChC,OAAO,IAAI,CAAC;YACd,CAAC;YACD,gDAAgD;YAChD,eAAe,GAAG,MAAM,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC;QAC3D,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAvBD,4BAuBC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nconst classPrototypeUuidSymbol: symbol = Symbol.for('TypeUuid.classPrototypeUuid');\n\nexport const uuidAlreadyReportedError: string = 'f26b0640-a49b-49d1-9ead-1a516d5920c7';\n\n// Avoid a dependency on node-core-library to access just this one API:\nexport class TypeUuid {\n /**\n * Returns true if the `targetObject` is an instance of a JavaScript class that was previously\n * registered using the specified `typeUuid`. Base classes are also considered.\n */\n public static isInstanceOf(targetObject: unknown, typeUuid: string): boolean {\n if (targetObject === undefined || targetObject === null) {\n return false;\n }\n\n let objectPrototype: {} = Object.getPrototypeOf(targetObject);\n while (objectPrototype !== undefined && objectPrototype !== null) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const registeredUuid: string = (objectPrototype as any)[classPrototypeUuidSymbol];\n if (registeredUuid === typeUuid) {\n return true;\n }\n // Walk upwards an examine base class prototypes\n objectPrototype = Object.getPrototypeOf(objectPrototype);\n }\n\n return false;\n }\n}\n"]}
|
package/lib/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.CommandLineHelper = exports.DynamicCommandLineParser = exports.CommandLineParser = exports.CommandLineParameterProvider = exports.CommandLineRemainder = exports.CommandLineChoiceListParameter = exports.CommandLineChoiceParameter = exports.CommandLineIntegerListParameter = exports.CommandLineIntegerParameter = exports.CommandLineStringListParameter = exports.CommandLineStringParameter = exports.CommandLineFlagParameter = exports.CommandLineParameterWithArgument = exports.CommandLineParameter = exports.CommandLineParameterKind = exports.AliasCommandLineAction = exports.ScopedCommandLineAction = exports.DynamicCommandLineAction = exports.CommandLineAction = void 0;
|
|
5
|
+
exports.CommandLineHelper = exports.CommandLineConstants = exports.DynamicCommandLineParser = exports.CommandLineParser = exports.CommandLineParameterProvider = exports.CommandLineRemainder = exports.CommandLineChoiceListParameter = exports.CommandLineChoiceParameter = exports.CommandLineIntegerListParameter = exports.CommandLineIntegerParameter = exports.CommandLineStringListParameter = exports.CommandLineStringParameter = exports.CommandLineFlagParameter = exports.CommandLineParameterWithArgument = exports.CommandLineParameter = exports.CommandLineParameterKind = exports.AliasCommandLineAction = exports.ScopedCommandLineAction = exports.DynamicCommandLineAction = exports.CommandLineAction = void 0;
|
|
6
6
|
/**
|
|
7
7
|
* An object-oriented command-line parser for TypeScript projects.
|
|
8
8
|
*
|
|
@@ -43,6 +43,8 @@ var CommandLineParser_1 = require("./providers/CommandLineParser");
|
|
|
43
43
|
Object.defineProperty(exports, "CommandLineParser", { enumerable: true, get: function () { return CommandLineParser_1.CommandLineParser; } });
|
|
44
44
|
var DynamicCommandLineParser_1 = require("./providers/DynamicCommandLineParser");
|
|
45
45
|
Object.defineProperty(exports, "DynamicCommandLineParser", { enumerable: true, get: function () { return DynamicCommandLineParser_1.DynamicCommandLineParser; } });
|
|
46
|
+
var Constants_1 = require("./Constants");
|
|
47
|
+
Object.defineProperty(exports, "CommandLineConstants", { enumerable: true, get: function () { return Constants_1.CommandLineConstants; } });
|
|
46
48
|
var CommandLineHelper_1 = require("./CommandLineHelper");
|
|
47
49
|
Object.defineProperty(exports, "CommandLineHelper", { enumerable: true, get: function () { return CommandLineHelper_1.CommandLineHelper; } });
|
|
48
50
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;GAIG;AAEH,mEAAkG;AAAzF,sHAAA,iBAAiB,OAAA;AAC1B,iFAAgF;AAAvE,oIAAA,wBAAwB,OAAA;AACjC,+EAA8E;AAArE,kIAAA,uBAAuB,OAAA;AAChC,6EAG4C;AAF1C,gIAAA,sBAAsB,OAAA;AAiBxB,wDAKkC;AAJhC,uHAAA,wBAAwB,OAAA;AACxB,iGAAiG;AACjG,mHAAA,wBAAwB,OAAwB;AAChD,+HAAA,gCAAgC,OAAA;AAGlC,kFAAiF;AAAxE,oIAAA,wBAAwB,OAAA;AACjC,sFAGiD;AAF/C,wIAAA,0BAA0B,OAAA;AAG5B,8FAA6F;AAApF,gJAAA,8BAA8B,OAAA;AACvC,wFAGkD;AAFhD,0IAAA,2BAA2B,OAAA;AAG7B,gGAA+F;AAAtF,kJAAA,+BAA+B,OAAA;AACxC,sFAGiD;AAF/C,wIAAA,0BAA0B,OAAA;AAG5B,8FAA6F;AAApF,gJAAA,8BAA8B,OAAA;AACvC,0EAAyE;AAAhE,4HAAA,oBAAoB,OAAA;AAE7B,yFAKkD;AAJhD,4IAAA,4BAA4B,OAAA;AAM9B,mEAAkG;AAAzF,sHAAA,iBAAiB,OAAA;AAC1B,iFAAgF;AAAvE,oIAAA,wBAAwB,OAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;GAIG;AAEH,mEAAkG;AAAzF,sHAAA,iBAAiB,OAAA;AAC1B,iFAAgF;AAAvE,oIAAA,wBAAwB,OAAA;AACjC,+EAA8E;AAArE,kIAAA,uBAAuB,OAAA;AAChC,6EAG4C;AAF1C,gIAAA,sBAAsB,OAAA;AAiBxB,wDAKkC;AAJhC,uHAAA,wBAAwB,OAAA;AACxB,iGAAiG;AACjG,mHAAA,wBAAwB,OAAwB;AAChD,+HAAA,gCAAgC,OAAA;AAGlC,kFAAiF;AAAxE,oIAAA,wBAAwB,OAAA;AACjC,sFAGiD;AAF/C,wIAAA,0BAA0B,OAAA;AAG5B,8FAA6F;AAApF,gJAAA,8BAA8B,OAAA;AACvC,wFAGkD;AAFhD,0IAAA,2BAA2B,OAAA;AAG7B,gGAA+F;AAAtF,kJAAA,+BAA+B,OAAA;AACxC,sFAGiD;AAF/C,wIAAA,0BAA0B,OAAA;AAG5B,8FAA6F;AAApF,gJAAA,8BAA8B,OAAA;AACvC,0EAAyE;AAAhE,4HAAA,oBAAoB,OAAA;AAE7B,yFAKkD;AAJhD,4IAAA,4BAA4B,OAAA;AAM9B,mEAAkG;AAAzF,sHAAA,iBAAiB,OAAA;AAC1B,iFAAgF;AAAvE,oIAAA,wBAAwB,OAAA;AAEjC,yCAAmD;AAA1C,iHAAA,oBAAoB,OAAA;AAE7B,yDAAwD;AAA/C,sHAAA,iBAAiB,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * An object-oriented command-line parser for TypeScript projects.\n *\n * @packageDocumentation\n */\n\nexport { CommandLineAction, type ICommandLineActionOptions } from './providers/CommandLineAction';\nexport { DynamicCommandLineAction } from './providers/DynamicCommandLineAction';\nexport { ScopedCommandLineAction } from './providers/ScopedCommandLineAction';\nexport {\n AliasCommandLineAction,\n type IAliasCommandLineActionOptions\n} from './providers/AliasCommandLineAction';\n\nexport type {\n IBaseCommandLineDefinition,\n IBaseCommandLineDefinitionWithArgument,\n ICommandLineFlagDefinition,\n ICommandLineStringDefinition,\n ICommandLineStringListDefinition,\n ICommandLineIntegerDefinition,\n ICommandLineIntegerListDefinition,\n ICommandLineChoiceDefinition,\n ICommandLineChoiceListDefinition,\n ICommandLineRemainderDefinition\n} from './parameters/CommandLineDefinition';\n\nexport {\n CommandLineParameterKind,\n // TODO: Export both `CommandLineParameter` and `CommandLineParameterBase` in the next major bump\n CommandLineParameterBase as CommandLineParameter,\n CommandLineParameterWithArgument\n} from './parameters/BaseClasses';\n\nexport { CommandLineFlagParameter } from './parameters/CommandLineFlagParameter';\nexport {\n CommandLineStringParameter,\n type IRequiredCommandLineStringParameter\n} from './parameters/CommandLineStringParameter';\nexport { CommandLineStringListParameter } from './parameters/CommandLineStringListParameter';\nexport {\n CommandLineIntegerParameter,\n type IRequiredCommandLineIntegerParameter\n} from './parameters/CommandLineIntegerParameter';\nexport { CommandLineIntegerListParameter } from './parameters/CommandLineIntegerListParameter';\nexport {\n CommandLineChoiceParameter,\n type IRequiredCommandLineChoiceParameter\n} from './parameters/CommandLineChoiceParameter';\nexport { CommandLineChoiceListParameter } from './parameters/CommandLineChoiceListParameter';\nexport { CommandLineRemainder } from './parameters/CommandLineRemainder';\n\nexport {\n CommandLineParameterProvider,\n type IScopedLongNameParseResult,\n type ICommandLineParserData as _ICommandLineParserData,\n type IRegisterDefinedParametersState as _IRegisterDefinedParametersState\n} from './providers/CommandLineParameterProvider';\n\nexport { CommandLineParser, type ICommandLineParserOptions } from './providers/CommandLineParser';\nexport { DynamicCommandLineParser } from './providers/DynamicCommandLineParser';\n\nexport { CommandLineConstants } from './Constants';\n\nexport { CommandLineHelper } from './CommandLineHelper';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommandLineParser.d.ts","sourceRoot":"","sources":["../../src/providers/CommandLineParser.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,QAAQ,MAAM,UAAU,CAAC;AAG1C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAE7D,OAAO,EACL,4BAA4B,EAC5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,iBAAiB,GAAG,SAAS;IAItE;;;;;;;;;;;;;;;;OAgBG;IACU,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IA+C5D;;OAEG;IAEU,OAAO,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAIvD;;;OAGG;IACU,gCAAgC,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAkG7E;;OAEG;IAEU,2BAA2B,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIxE,gBAAgB;IACT,0BAA0B,CAAC,KAAK,EAAE,+BAA+B,GAAG,IAAI;IAkB/E,OAAO,CAAC,oBAAoB;IAO5B;;;OAGG;IACH,SAAS,CAAC,kBAAkB,IAAI,QAAQ,CAAC,cAAc;IAKvD;;;OAGG;cACa,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;CAK3C"}
|
|
@@ -7,6 +7,7 @@ const terminal_1 = require("@rushstack/terminal");
|
|
|
7
7
|
const CommandLineParameterProvider_1 = require("./CommandLineParameterProvider");
|
|
8
8
|
const CommandLineParserExitError_1 = require("./CommandLineParserExitError");
|
|
9
9
|
const TabCompletionAction_1 = require("./TabCompletionAction");
|
|
10
|
+
const TypeUuidLite_1 = require("../TypeUuidLite");
|
|
10
11
|
/**
|
|
11
12
|
* The "argparse" library is a relatively advanced command-line parser with features such
|
|
12
13
|
* as word-wrapping and intelligible error messages (that are lacking in other similar
|
|
@@ -110,6 +111,12 @@ class CommandLineParser extends CommandLineParameterProvider_1.CommandLineParame
|
|
|
110
111
|
process.exitCode = err.exitCode;
|
|
111
112
|
}
|
|
112
113
|
}
|
|
114
|
+
else if (TypeUuidLite_1.TypeUuid.isInstanceOf(err, TypeUuidLite_1.uuidAlreadyReportedError)) {
|
|
115
|
+
// AlreadyReportedError
|
|
116
|
+
if (!process.exitCode) {
|
|
117
|
+
process.exitCode = 1;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
113
120
|
else {
|
|
114
121
|
let message = (err.message || 'An unknown error occurred').trim();
|
|
115
122
|
// If the message doesn't already start with "Error:" then add a prefix
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommandLineParser.js","sourceRoot":"","sources":["../../src/providers/CommandLineParser.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAG3D,kDAA+C;AAI/C,iFAIwC;AACxC,6EAAgG;AAChG,+DAA0D;AA6B1D;;;;;;;;;GASG;AACH,MAAsB,iBAAkB,SAAQ,2DAA4B;IAgB1E,YAAmB,OAAkC;;QACnD,KAAK,EAAE,CAAC;QAJF,cAAS,GAAY,KAAK,CAAC;QAC3B,+BAA0B,GAAY,KAAK,CAAC;QAKlD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,cAAc,GAAG,IAAI,GAAG,EAA6B,CAAC;QAE3D,IAAI,CAAC,eAAe,GAAG,IAAI,iDAAoB,CAAC;YAC9C,OAAO,EAAE,IAAI;YACb,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,YAAY;YAChC,WAAW,EAAE,IAAI,CAAC,QAAQ,CAAC,eAAe;YAC1C,MAAM,EAAE,mBAAQ,CAAC,IAAI,CACnB,MAAA,IAAI,CAAC,QAAQ,CAAC,UAAU,mCACtB,oDAAoD,IAAI,CAAC,QAAQ,CAAC,YAAY,eAAe,CAChG;SACF,CAAC,CAAC;QAEH,MAAA,IAAI,CAAC,kBAAkB,oDAAI,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,MAAyB;QACxC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;Y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M,CAAC,0BAA0B,CAAC,WAAW,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;IAEO,oBAAoB;QAC1B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC9C,+CAA+C;YAC/C,MAAM,IAAI,KAAK,CAAC,oFAAoF,CAAC,CAAC;QACxG,CAAC;IACH,CAAC;IAED;;;OAGG;IACO,kBAAkB;QAC1B,WAAW;QACX,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACO,KAAK,CAAC,SAAS;QACvB,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxB,MAAM,IAAI,CAAC,cAAc,CAAC,aAAa,EAAE,CAAC;QAC5C,CAAC;IACH,CAAC;CACF;AA5SD,8CA4SC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type * as argparse from 'argparse';\nimport { Colorize } from '@rushstack/terminal';\n\nimport type { CommandLineAction } from './CommandLineAction';\nimport type { AliasCommandLineAction } from './AliasCommandLineAction';\nimport {\n CommandLineParameterProvider,\n type IRegisterDefinedParametersState,\n type ICommandLineParserData\n} from './CommandLineParameterProvider';\nimport { CommandLineParserExitError, CustomArgumentParser } from './CommandLineParserExitError';\nimport { TabCompleteAction } from './TabCompletionAction';\n\n/**\n * Options for the {@link CommandLineParser} constructor.\n * @public\n */\nexport interface ICommandLineParserOptions {\n /**\n * The name of your tool when invoked from the command line\n */\n toolFilename: string;\n\n /**\n * General documentation that is included in the \"--help\" main page\n */\n toolDescription: string;\n\n /**\n * An optional string to append at the end of the \"--help\" main page. If not provided, an epilog\n * will be automatically generated based on the toolFilename.\n */\n toolEpilog?: string;\n\n /**\n * Set to true to auto-define a tab completion action. False by default.\n */\n enableTabCompletionAction?: boolean;\n}\n\n/**\n * The \"argparse\" library is a relatively advanced command-line parser with features such\n * as word-wrapping and intelligible error messages (that are lacking in other similar\n * libraries such as commander, yargs, and nomnom). Unfortunately, its ruby-inspired API\n * is awkward to use. The abstract base classes CommandLineParser and CommandLineAction\n * provide a wrapper for \"argparse\" that makes defining and consuming arguments quick\n * and simple, and enforces that appropriate documentation is provided for each parameter.\n *\n * @public\n */\nexport abstract class CommandLineParser extends CommandLineParameterProvider {\n /**\n * Reports which CommandLineAction was specified on the command line.\n * @remarks\n * The value will be assigned before onExecute() is invoked.\n */\n public selectedAction: CommandLineAction | undefined;\n\n private readonly _argumentParser: argparse.ArgumentParser;\n private _actionsSubParser: argparse.SubParser | undefined;\n private readonly _options: ICommandLineParserOptions;\n private readonly _actions: CommandLineAction[];\n private readonly _actionsByName: Map<string, CommandLineAction>;\n private _executed: boolean = false;\n private _tabCompleteActionWasAdded: boolean = false;\n\n public constructor(options: ICommandLineParserOptions) {\n super();\n\n this._options = options;\n this._actions = [];\n this._actionsByName = new Map<string, CommandLineAction>();\n\n this._argumentParser = new CustomArgumentParser({\n addHelp: true,\n prog: this._options.toolFilename,\n description: this._options.toolDescription,\n epilog: Colorize.bold(\n this._options.toolEpilog ??\n `For detailed help about a specific command, use: ${this._options.toolFilename} <command> -h`\n )\n });\n\n this.onDefineParameters?.();\n }\n\n /**\n * Returns the list of actions that were defined for this CommandLineParser object.\n */\n public get actions(): ReadonlyArray<CommandLineAction> {\n return this._actions;\n }\n\n /**\n * Defines a new action that can be used with the CommandLineParser instance.\n */\n public addAction(action: CommandLineAction): void {\n if (!this._actionsSubParser) {\n this._actionsSubParser = this._argumentParser.addSubparsers({\n metavar: '<command>',\n dest: 'action'\n });\n }\n\n action._buildParser(this._actionsSubParser);\n this._actions.push(action);\n this._actionsByName.set(action.actionName, action);\n }\n\n /**\n * Retrieves the action with the specified name. If no matching action is found,\n * an exception is thrown.\n */\n public getAction(actionName: string): CommandLineAction {\n const action: CommandLineAction | undefined = this.tryGetAction(actionName);\n if (!action) {\n throw new Error(`The action \"${actionName}\" was not defined`);\n }\n return action;\n }\n\n /**\n * Retrieves the action with the specified name. If no matching action is found,\n * undefined is returned.\n */\n public tryGetAction(actionName: string): CommandLineAction | undefined {\n return this._actionsByName.get(actionName);\n }\n\n /**\n * The program entry point will call this method to begin parsing command-line arguments\n * and executing the corresponding action.\n *\n * @remarks\n * The returned promise will never reject: If an error occurs, it will be printed\n * to stderr, process.exitCode will be set to 1, and the promise will resolve to false.\n * This simplifies the most common usage scenario where the program entry point doesn't\n * want to be involved with the command-line logic, and will discard the promise without\n * a then() or catch() block.\n *\n * If your caller wants to trap and handle errors, use {@link CommandLineParser.executeWithoutErrorHandlingAsync}\n * instead.\n *\n * @param args - the command-line arguments to be parsed; if omitted, then\n * the process.argv will be used\n */\n public async executeAsync(args?: string[]): Promise<boolean> {\n if (this._options.enableTabCompletionAction && !this._tabCompleteActionWasAdded) {\n this.addAction(new TabCompleteAction(this.actions, this.parameters));\n this._tabCompleteActionWasAdded = true;\n }\n\n try {\n await this.executeWithoutErrorHandlingAsync(args);\n return true;\n } catch (err) {\n if (err instanceof CommandLineParserExitError) {\n // executeWithoutErrorHandlingAsync() handles the successful cases,\n // so here we can assume err has a nonzero exit code\n if (err.message) {\n // eslint-disable-next-line no-console\n console.error(err.message);\n }\n if (!process.exitCode) {\n process.exitCode = err.exitCode;\n }\n } else {\n let message: string = ((err as Error).message || 'An unknown error occurred').trim();\n\n // If the message doesn't already start with \"Error:\" then add a prefix\n if (!/^(error|internal error|warning)\\b/i.test(message)) {\n message = 'Error: ' + message;\n }\n\n // eslint-disable-next-line no-console\n console.error();\n // eslint-disable-next-line no-console\n console.error(Colorize.red(message));\n\n if (!process.exitCode) {\n process.exitCode = 1;\n }\n }\n\n return false;\n }\n }\n\n /**\n * @deprecated Use {@link CommandLineParser.executeAsync} instead.\n */\n // eslint-disable-next-line @typescript-eslint/naming-convention\n public async execute(args?: string[]): Promise<boolean> {\n return await this.executeAsync(args);\n }\n\n /**\n * This is similar to {@link CommandLineParser.executeAsync}, except that execution errors\n * simply cause the promise to reject. It is the caller's responsibility to trap\n */\n public async executeWithoutErrorHandlingAsync(args?: string[]): Promise<void> {\n try {\n if (this._executed) {\n // In the future we could allow the same parser to be invoked multiple times\n // with different arguments. We'll do that work as soon as someone encounters\n // a real world need for it.\n throw new Error('executeAsync() was already called for this parser instance');\n }\n this._executed = true;\n\n this._validateDefinitions();\n\n // Register the parameters before we print help or parse the CLI\n const initialState: IRegisterDefinedParametersState = {\n parentParameterNames: new Set()\n };\n this._registerDefinedParameters(initialState);\n\n if (!args) {\n // 0=node.exe, 1=script name\n args = process.argv.slice(2);\n }\n if (this.actions.length > 0) {\n if (args.length === 0) {\n // Parsers that use actions should print help when 0 args are provided. Allow\n // actionless parsers to continue on zero args.\n this._argumentParser.printHelp();\n return;\n }\n // Alias actions may provide a list of default params to add after the action name.\n // Since we don't know which params are required and which are optional, perform a\n // manual search for the action name to obtain the default params and insert them if\n // any are found. We will guess that the action name is the first arg that doesn't\n // start with a hyphen.\n const actionNameIndex: number | undefined = args.findIndex((x) => !x.startsWith('-'));\n if (actionNameIndex !== undefined) {\n const actionName: string = args[actionNameIndex];\n const action: CommandLineAction | undefined = this.tryGetAction(actionName);\n const aliasAction: AliasCommandLineAction | undefined = action as AliasCommandLineAction;\n if (aliasAction?.defaultParameters?.length) {\n const insertIndex: number = actionNameIndex + 1;\n args = args.slice(0, insertIndex).concat(aliasAction.defaultParameters, args.slice(insertIndex));\n }\n }\n }\n\n const postParse: () => void = () => {\n this._postParse();\n for (const action of this.actions) {\n action._postParse();\n }\n };\n\n function patchFormatUsageForArgumentParser(argumentParser: argparse.ArgumentParser): void {\n const originalFormatUsage: () => string = argumentParser.formatUsage.bind(argumentParser);\n argumentParser.formatUsage = () => {\n postParse();\n return originalFormatUsage();\n };\n }\n\n this._preParse();\n patchFormatUsageForArgumentParser(this._argumentParser);\n for (const action of this.actions) {\n action._preParse();\n patchFormatUsageForArgumentParser(action._getArgumentParser());\n }\n\n const data: ICommandLineParserData = this._argumentParser.parseArgs(args);\n\n postParse();\n this._processParsedData(this._options, data);\n\n this.selectedAction = this.tryGetAction(data.action);\n if (this.actions.length > 0 && !this.selectedAction) {\n const actions: string[] = this.actions.map((x) => x.actionName);\n throw new Error(`An action must be specified (${actions.join(', ')})`);\n }\n\n this.selectedAction?._processParsedData(this._options, data);\n await this.onExecute();\n } catch (err) {\n if (err instanceof CommandLineParserExitError) {\n if (!err.exitCode) {\n // non-error exit modeled using exception handling\n if (err.message) {\n // eslint-disable-next-line no-console\n console.log(err.message);\n }\n\n return;\n }\n }\n\n throw err;\n }\n }\n\n /**\n * @deprecated Use {@link CommandLineParser.executeWithoutErrorHandlingAsync} instead.\n */\n // eslint-disable-next-line @typescript-eslint/naming-convention\n public async executeWithoutErrorHandling(args?: string[]): Promise<void> {\n await this.executeWithoutErrorHandlingAsync(args);\n }\n\n /** @internal */\n public _registerDefinedParameters(state: IRegisterDefinedParametersState): void {\n super._registerDefinedParameters(state);\n\n const { parentParameterNames } = state;\n const updatedParentParameterNames: Set<string> = new Set([\n ...parentParameterNames,\n ...this._registeredParameterParserKeysByName.keys()\n ]);\n\n const parentState: IRegisterDefinedParametersState = {\n ...state,\n parentParameterNames: updatedParentParameterNames\n };\n for (const action of this._actions) {\n action._registerDefinedParameters(parentState);\n }\n }\n\n private _validateDefinitions(): void {\n if (this.remainder && this.actions.length > 0) {\n // This is apparently not supported by argparse\n throw new Error('defineCommandLineRemainder() cannot be called for a CommandLineParser with actions');\n }\n }\n\n /**\n * {@inheritDoc CommandLineParameterProvider._getArgumentParser}\n * @internal\n */\n protected _getArgumentParser(): argparse.ArgumentParser {\n // override\n return this._argumentParser;\n }\n\n /**\n * This hook allows the subclass to perform additional operations before or after\n * the chosen action is executed.\n */\n protected async onExecute(): Promise<void> {\n if (this.selectedAction) {\n await this.selectedAction._executeAsync();\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"CommandLineParser.js","sourceRoot":"","sources":["../../src/providers/CommandLineParser.ts"],"names":[],"mappings":";AAAA,4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,CAAC;IACH,CAAC;IAEO,oBAAoB;QAC1B,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC9C,+CAA+C;YAC/C,MAAM,IAAI,KAAK,CAAC,oFAAoF,CAAC,CAAC;QACxG,CAAC;IACH,CAAC;IAED;;;OAGG;IACO,kBAAkB;QAC1B,WAAW;QACX,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACO,KAAK,CAAC,SAAS;QACvB,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxB,MAAM,IAAI,CAAC,cAAc,CAAC,aAAa,EAAE,CAAC;QAC5C,CAAC;IACH,CAAC;CACF;AAjTD,8CAiTC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type * as argparse from 'argparse';\nimport { Colorize } from '@rushstack/terminal';\n\nimport type { CommandLineAction } from './CommandLineAction';\nimport type { AliasCommandLineAction } from './AliasCommandLineAction';\nimport {\n CommandLineParameterProvider,\n type IRegisterDefinedParametersState,\n type ICommandLineParserData\n} from './CommandLineParameterProvider';\nimport { CommandLineParserExitError, CustomArgumentParser } from './CommandLineParserExitError';\nimport { TabCompleteAction } from './TabCompletionAction';\nimport { TypeUuid, uuidAlreadyReportedError } from '../TypeUuidLite';\n\n/**\n * Options for the {@link CommandLineParser} constructor.\n * @public\n */\nexport interface ICommandLineParserOptions {\n /**\n * The name of your tool when invoked from the command line\n */\n toolFilename: string;\n\n /**\n * General documentation that is included in the \"--help\" main page\n */\n toolDescription: string;\n\n /**\n * An optional string to append at the end of the \"--help\" main page. If not provided, an epilog\n * will be automatically generated based on the toolFilename.\n */\n toolEpilog?: string;\n\n /**\n * Set to true to auto-define a tab completion action. False by default.\n */\n enableTabCompletionAction?: boolean;\n}\n\n/**\n * The \"argparse\" library is a relatively advanced command-line parser with features such\n * as word-wrapping and intelligible error messages (that are lacking in other similar\n * libraries such as commander, yargs, and nomnom). Unfortunately, its ruby-inspired API\n * is awkward to use. The abstract base classes CommandLineParser and CommandLineAction\n * provide a wrapper for \"argparse\" that makes defining and consuming arguments quick\n * and simple, and enforces that appropriate documentation is provided for each parameter.\n *\n * @public\n */\nexport abstract class CommandLineParser extends CommandLineParameterProvider {\n /**\n * Reports which CommandLineAction was specified on the command line.\n * @remarks\n * The value will be assigned before onExecute() is invoked.\n */\n public selectedAction: CommandLineAction | undefined;\n\n private readonly _argumentParser: argparse.ArgumentParser;\n private _actionsSubParser: argparse.SubParser | undefined;\n private readonly _options: ICommandLineParserOptions;\n private readonly _actions: CommandLineAction[];\n private readonly _actionsByName: Map<string, CommandLineAction>;\n private _executed: boolean = false;\n private _tabCompleteActionWasAdded: boolean = false;\n\n public constructor(options: ICommandLineParserOptions) {\n super();\n\n this._options = options;\n this._actions = [];\n this._actionsByName = new Map<string, CommandLineAction>();\n\n this._argumentParser = new CustomArgumentParser({\n addHelp: true,\n prog: this._options.toolFilename,\n description: this._options.toolDescription,\n epilog: Colorize.bold(\n this._options.toolEpilog ??\n `For detailed help about a specific command, use: ${this._options.toolFilename} <command> -h`\n )\n });\n\n this.onDefineParameters?.();\n }\n\n /**\n * Returns the list of actions that were defined for this CommandLineParser object.\n */\n public get actions(): ReadonlyArray<CommandLineAction> {\n return this._actions;\n }\n\n /**\n * Defines a new action that can be used with the CommandLineParser instance.\n */\n public addAction(action: CommandLineAction): void {\n if (!this._actionsSubParser) {\n this._actionsSubParser = this._argumentParser.addSubparsers({\n metavar: '<command>',\n dest: 'action'\n });\n }\n\n action._buildParser(this._actionsSubParser);\n this._actions.push(action);\n this._actionsByName.set(action.actionName, action);\n }\n\n /**\n * Retrieves the action with the specified name. If no matching action is found,\n * an exception is thrown.\n */\n public getAction(actionName: string): CommandLineAction {\n const action: CommandLineAction | undefined = this.tryGetAction(actionName);\n if (!action) {\n throw new Error(`The action \"${actionName}\" was not defined`);\n }\n return action;\n }\n\n /**\n * Retrieves the action with the specified name. If no matching action is found,\n * undefined is returned.\n */\n public tryGetAction(actionName: string): CommandLineAction | undefined {\n return this._actionsByName.get(actionName);\n }\n\n /**\n * The program entry point will call this method to begin parsing command-line arguments\n * and executing the corresponding action.\n *\n * @remarks\n * The returned promise will never reject: If an error occurs, it will be printed\n * to stderr, process.exitCode will be set to 1, and the promise will resolve to false.\n * This simplifies the most common usage scenario where the program entry point doesn't\n * want to be involved with the command-line logic, and will discard the promise without\n * a then() or catch() block.\n *\n * If your caller wants to trap and handle errors, use {@link CommandLineParser.executeWithoutErrorHandlingAsync}\n * instead.\n *\n * @param args - the command-line arguments to be parsed; if omitted, then\n * the process.argv will be used\n */\n public async executeAsync(args?: string[]): Promise<boolean> {\n if (this._options.enableTabCompletionAction && !this._tabCompleteActionWasAdded) {\n this.addAction(new TabCompleteAction(this.actions, this.parameters));\n this._tabCompleteActionWasAdded = true;\n }\n\n try {\n await this.executeWithoutErrorHandlingAsync(args);\n return true;\n } catch (err) {\n if (err instanceof CommandLineParserExitError) {\n // executeWithoutErrorHandlingAsync() handles the successful cases,\n // so here we can assume err has a nonzero exit code\n if (err.message) {\n // eslint-disable-next-line no-console\n console.error(err.message);\n }\n if (!process.exitCode) {\n process.exitCode = err.exitCode;\n }\n } else if (TypeUuid.isInstanceOf(err, uuidAlreadyReportedError)) {\n // AlreadyReportedError\n if (!process.exitCode) {\n process.exitCode = 1;\n }\n } else {\n let message: string = ((err as Error).message || 'An unknown error occurred').trim();\n\n // If the message doesn't already start with \"Error:\" then add a prefix\n if (!/^(error|internal error|warning)\\b/i.test(message)) {\n message = 'Error: ' + message;\n }\n\n // eslint-disable-next-line no-console\n console.error();\n // eslint-disable-next-line no-console\n console.error(Colorize.red(message));\n\n if (!process.exitCode) {\n process.exitCode = 1;\n }\n }\n\n return false;\n }\n }\n\n /**\n * @deprecated Use {@link CommandLineParser.executeAsync} instead.\n */\n // eslint-disable-next-line @typescript-eslint/naming-convention\n public async execute(args?: string[]): Promise<boolean> {\n return await this.executeAsync(args);\n }\n\n /**\n * This is similar to {@link CommandLineParser.executeAsync}, except that execution errors\n * simply cause the promise to reject. It is the caller's responsibility to trap\n */\n public async executeWithoutErrorHandlingAsync(args?: string[]): Promise<void> {\n try {\n if (this._executed) {\n // In the future we could allow the same parser to be invoked multiple times\n // with different arguments. We'll do that work as soon as someone encounters\n // a real world need for it.\n throw new Error('executeAsync() was already called for this parser instance');\n }\n this._executed = true;\n\n this._validateDefinitions();\n\n // Register the parameters before we print help or parse the CLI\n const initialState: IRegisterDefinedParametersState = {\n parentParameterNames: new Set()\n };\n this._registerDefinedParameters(initialState);\n\n if (!args) {\n // 0=node.exe, 1=script name\n args = process.argv.slice(2);\n }\n if (this.actions.length > 0) {\n if (args.length === 0) {\n // Parsers that use actions should print help when 0 args are provided. Allow\n // actionless parsers to continue on zero args.\n this._argumentParser.printHelp();\n return;\n }\n // Alias actions may provide a list of default params to add after the action name.\n // Since we don't know which params are required and which are optional, perform a\n // manual search for the action name to obtain the default params and insert them if\n // any are found. We will guess that the action name is the first arg that doesn't\n // start with a hyphen.\n const actionNameIndex: number | undefined = args.findIndex((x) => !x.startsWith('-'));\n if (actionNameIndex !== undefined) {\n const actionName: string = args[actionNameIndex];\n const action: CommandLineAction | undefined = this.tryGetAction(actionName);\n const aliasAction: AliasCommandLineAction | undefined = action as AliasCommandLineAction;\n if (aliasAction?.defaultParameters?.length) {\n const insertIndex: number = actionNameIndex + 1;\n args = args.slice(0, insertIndex).concat(aliasAction.defaultParameters, args.slice(insertIndex));\n }\n }\n }\n\n const postParse: () => void = () => {\n this._postParse();\n for (const action of this.actions) {\n action._postParse();\n }\n };\n\n function patchFormatUsageForArgumentParser(argumentParser: argparse.ArgumentParser): void {\n const originalFormatUsage: () => string = argumentParser.formatUsage.bind(argumentParser);\n argumentParser.formatUsage = () => {\n postParse();\n return originalFormatUsage();\n };\n }\n\n this._preParse();\n patchFormatUsageForArgumentParser(this._argumentParser);\n for (const action of this.actions) {\n action._preParse();\n patchFormatUsageForArgumentParser(action._getArgumentParser());\n }\n\n const data: ICommandLineParserData = this._argumentParser.parseArgs(args);\n\n postParse();\n this._processParsedData(this._options, data);\n\n this.selectedAction = this.tryGetAction(data.action);\n if (this.actions.length > 0 && !this.selectedAction) {\n const actions: string[] = this.actions.map((x) => x.actionName);\n throw new Error(`An action must be specified (${actions.join(', ')})`);\n }\n\n this.selectedAction?._processParsedData(this._options, data);\n await this.onExecute();\n } catch (err) {\n if (err instanceof CommandLineParserExitError) {\n if (!err.exitCode) {\n // non-error exit modeled using exception handling\n if (err.message) {\n // eslint-disable-next-line no-console\n console.log(err.message);\n }\n\n return;\n }\n }\n\n throw err;\n }\n }\n\n /**\n * @deprecated Use {@link CommandLineParser.executeWithoutErrorHandlingAsync} instead.\n */\n // eslint-disable-next-line @typescript-eslint/naming-convention\n public async executeWithoutErrorHandling(args?: string[]): Promise<void> {\n await this.executeWithoutErrorHandlingAsync(args);\n }\n\n /** @internal */\n public _registerDefinedParameters(state: IRegisterDefinedParametersState): void {\n super._registerDefinedParameters(state);\n\n const { parentParameterNames } = state;\n const updatedParentParameterNames: Set<string> = new Set([\n ...parentParameterNames,\n ...this._registeredParameterParserKeysByName.keys()\n ]);\n\n const parentState: IRegisterDefinedParametersState = {\n ...state,\n parentParameterNames: updatedParentParameterNames\n };\n for (const action of this._actions) {\n action._registerDefinedParameters(parentState);\n }\n }\n\n private _validateDefinitions(): void {\n if (this.remainder && this.actions.length > 0) {\n // This is apparently not supported by argparse\n throw new Error('defineCommandLineRemainder() cannot be called for a CommandLineParser with actions');\n }\n }\n\n /**\n * {@inheritDoc CommandLineParameterProvider._getArgumentParser}\n * @internal\n */\n protected _getArgumentParser(): argparse.ArgumentParser {\n // override\n return this._argumentParser;\n }\n\n /**\n * This hook allows the subclass to perform additional operations before or after\n * the chosen action is executed.\n */\n protected async onExecute(): Promise<void> {\n if (this.selectedAction) {\n await this.selectedAction._executeAsync();\n }\n }\n}\n"]}
|
|
@@ -35,12 +35,13 @@ const string_argv_1 = __importDefault(require("string-argv"));
|
|
|
35
35
|
const BaseClasses_1 = require("../parameters/BaseClasses");
|
|
36
36
|
const CommandLineChoiceParameter_1 = require("../parameters/CommandLineChoiceParameter");
|
|
37
37
|
const CommandLineAction_1 = require("./CommandLineAction");
|
|
38
|
+
const Constants_1 = require("../Constants");
|
|
38
39
|
const DEFAULT_WORD_TO_AUTOCOMPLETE = '';
|
|
39
40
|
const DEFAULT_POSITION = 0;
|
|
40
41
|
class TabCompleteAction extends CommandLineAction_1.CommandLineAction {
|
|
41
42
|
constructor(actions, globalParameters) {
|
|
42
43
|
super({
|
|
43
|
-
actionName:
|
|
44
|
+
actionName: Constants_1.CommandLineConstants.TabCompletionActionName,
|
|
44
45
|
summary: 'Provides tab completion.',
|
|
45
46
|
documentation: 'Provides tab completion.'
|
|
46
47
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TabCompletionAction.js","sourceRoot":"","sources":["../../src/providers/TabCompletionAction.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,8DAAqC;AAIrC,2DAKmC;AACnC,yFAAsF;AACtF,2DAAwD;AAGxD,MAAM,4BAA4B,GAAW,EAAE,CAAC;AAChD,MAAM,gBAAgB,GAAW,CAAC,CAAC;AAEnC,MAAa,iBAAkB,SAAQ,qCAAiB;IAMtD,YACE,OAAyC,EACzC,gBAAyD;QAEzD,KAAK,CAAC;YACJ,UAAU,mEAA8C;YACxD,OAAO,EAAE,0BAA0B;YACnC,aAAa,EAAE,0BAA0B;SAC1C,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;QAC1B,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,MAAM,+BAA+B,GAAsC,IAAI,GAAG,EAAE,CAAC;YACrF,KAAK,MAAM,SAAS,IAAI,MA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BAClD,MAAM,oBAAoB,CAAC;YAC7B,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA9MD,8CA8MC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport stringArgv from 'string-argv';\n\nimport type { IRequiredCommandLineIntegerParameter } from '../parameters/CommandLineIntegerParameter';\nimport type { IRequiredCommandLineStringParameter } from '../parameters/CommandLineStringParameter';\nimport {\n CommandLineParameterKind,\n type CommandLineParameterBase,\n CommandLineParameterWithArgument,\n type CommandLineParameter\n} from '../parameters/BaseClasses';\nimport { CommandLineChoiceParameter } from '../parameters/CommandLineChoiceParameter';\nimport { CommandLineAction } from './CommandLineAction';\nimport { CommandLineConstants } from '../Constants';\n\nconst DEFAULT_WORD_TO_AUTOCOMPLETE: string = '';\nconst DEFAULT_POSITION: number = 0;\n\nexport class TabCompleteAction extends CommandLineAction {\n private readonly _wordToCompleteParameter: IRequiredCommandLineStringParameter;\n private readonly _positionParameter: IRequiredCommandLineIntegerParameter;\n private readonly _actions: Map<string, Map<string, CommandLineParameter>>;\n private readonly _globalParameters: Map<string, CommandLineParameter>;\n\n public constructor(\n actions: ReadonlyArray<CommandLineAction>,\n globalParameters: ReadonlyArray<CommandLineParameterBase>\n ) {\n super({\n actionName: CommandLineConstants.TabCompletionActionName,\n summary: 'Provides tab completion.',\n documentation: 'Provides tab completion.'\n });\n\n this._actions = new Map();\n for (const action of actions) {\n const parameterNameToParameterInfoMap: Map<string, CommandLineParameter> = new Map();\n for (const parameter of action.parameters) {\n parameterNameToParameterInfoMap.set(parameter.longName, parameter as CommandLineParameter);\n if (parameter.shortName) {\n parameterNameToParameterInfoMap.set(parameter.shortName, parameter as CommandLineParameter);\n }\n }\n this._actions.set(action.actionName, parameterNameToParameterInfoMap);\n }\n\n this._globalParameters = new Map<string, CommandLineParameter>();\n for (const parameter of globalParameters) {\n this._globalParameters.set(parameter.longName, parameter as CommandLineParameter);\n if (parameter.shortName) {\n this._globalParameters.set(parameter.shortName, parameter as CommandLineParameter);\n }\n }\n\n this._wordToCompleteParameter = this.defineStringParameter({\n parameterLongName: '--word',\n argumentName: 'WORD',\n description: `The word to complete.`,\n defaultValue: DEFAULT_WORD_TO_AUTOCOMPLETE\n });\n\n this._positionParameter = this.defineIntegerParameter({\n parameterLongName: '--position',\n argumentName: 'INDEX',\n description: `The position in the word to be completed.`,\n defaultValue: DEFAULT_POSITION\n });\n }\n\n protected async onExecute(): Promise<void> {\n const commandLine: string = this._wordToCompleteParameter.value;\n const caretPosition: number = this._positionParameter.value || commandLine.length;\n\n for await (const value of this.getCompletionsAsync(commandLine, caretPosition)) {\n // eslint-disable-next-line no-console\n console.log(value);\n }\n }\n\n public async *getCompletionsAsync(\n commandLine: string,\n caretPosition: number = commandLine.length\n ): AsyncIterable<string> {\n const actions: Map<string, Map<string, CommandLineParameter>> = this._actions;\n\n if (!commandLine || !caretPosition) {\n yield* this._getAllActions();\n return;\n }\n\n const tokens: string[] = Array.from(this.tokenizeCommandLine(commandLine));\n\n // offset arguments by the number of global params in the input\n const globalParameterOffset: number = this._getGlobalParameterOffset(tokens);\n\n if (tokens.length < 2 + globalParameterOffset) {\n yield* this._getAllActions();\n return;\n }\n\n const lastToken: string = tokens[tokens.length - 1];\n const secondLastToken: string = tokens[tokens.length - 2];\n\n const lastCharacterIsWhitespace: boolean = !commandLine.slice(-1).trim();\n const completePartialWord: boolean = caretPosition === commandLine.length && !lastCharacterIsWhitespace;\n\n if (completePartialWord && tokens.length === 2 + globalParameterOffset) {\n for (const actionName of actions.keys()) {\n if (actionName.indexOf(tokens[1 + globalParameterOffset]) === 0) {\n yield actionName;\n }\n }\n } else {\n for (const actionName of actions.keys()) {\n if (actionName === tokens[1 + globalParameterOffset]) {\n const parameterNameMap: Map<string, CommandLineParameter> = actions.get(actionName)!;\n\n const parameterNames: string[] = Array.from(parameterNameMap.keys());\n\n if (completePartialWord) {\n for (const parameterName of parameterNames) {\n if (parameterName === secondLastToken) {\n const values: ReadonlyArray<string> = await this._getParameterValueCompletionsAsync(\n parameterNameMap.get(parameterName)!\n );\n if (values.length > 0) {\n yield* this._completeParameterValues(values, lastToken);\n return;\n }\n }\n }\n yield* this._completeParameterValues(parameterNames, lastToken);\n } else {\n for (const parameterName of parameterNames) {\n if (parameterName === lastToken) {\n const values: ReadonlyArray<string> = await this._getParameterValueCompletionsAsync(\n parameterNameMap.get(parameterName)!\n );\n if (values.length > 0) {\n yield* values;\n return;\n }\n }\n }\n for (const parameterName of parameterNames) {\n if (\n parameterName === lastToken &&\n parameterNameMap.get(parameterName)!.kind !== CommandLineParameterKind.Flag\n ) {\n // The parameter is expecting a value, so don't suggest parameter names again\n return;\n }\n }\n\n yield* parameterNames;\n }\n\n break;\n }\n }\n }\n }\n\n private *_getAllActions(): IterableIterator<string> {\n yield* this._actions.keys();\n yield* this._globalParameters.keys();\n }\n\n public tokenizeCommandLine(commandLine: string): string[] {\n return stringArgv(commandLine);\n }\n\n private async _getParameterValueCompletionsAsync(\n parameter: CommandLineParameter\n ): Promise<ReadonlyArray<string>> {\n let choiceParameterValues: ReadonlyArray<string> = [];\n if (parameter.kind === CommandLineParameterKind.Choice) {\n choiceParameterValues = parameter.alternatives;\n } else if (parameter.kind !== CommandLineParameterKind.Flag) {\n let parameterWithArgumentOrChoices:\n | CommandLineParameterWithArgument\n | CommandLineChoiceParameter\n | undefined = undefined;\n if (\n parameter instanceof CommandLineParameterWithArgument ||\n parameter instanceof CommandLineChoiceParameter\n ) {\n parameterWithArgumentOrChoices = parameter;\n }\n\n if (parameterWithArgumentOrChoices?.completions) {\n choiceParameterValues = await parameterWithArgumentOrChoices.completions();\n }\n }\n\n return choiceParameterValues;\n }\n\n private _getGlobalParameterOffset(tokens: string[]): number {\n const globalParameters: Map<string, CommandLineParameter> = this._globalParameters;\n let count: number = 0;\n\n outer: for (let i: number = 1; i < tokens.length; i++) {\n for (const globalParameter of globalParameters.values()) {\n if (tokens[i] !== globalParameter.longName && tokens[i] !== globalParameter.shortName) {\n break outer;\n }\n }\n count++;\n }\n\n return count;\n }\n\n private *_completeParameterValues(\n choiceParameterValues: ReadonlyArray<string>,\n lastToken: string\n ): IterableIterator<string> {\n for (const choiceParameterValue of choiceParameterValues) {\n if (choiceParameterValue.indexOf(lastToken) === 0) {\n yield choiceParameterValue;\n }\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TabCompletionAction.js","sourceRoot":"","sources":["../../src/providers/TabCompletionAction.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,8DAAqC;AAIrC,2DAKmC;AACnC,yFAAsF;AACtF,2DAAwD;AACxD,4CAAoD;AAEpD,MAAM,4BAA4B,GAAW,EAAE,CAAC;AAChD,MAAM,gBAAgB,GAAW,CAAC,CAAC;AAEnC,MAAa,iBAAkB,SAAQ,qCAAiB;IAMtD,YACE,OAAyC,EACzC,gBAAyD;QAEzD,KAAK,CAAC;YACJ,UAAU,EAAE,gCAAoB,CAAC,uBAAuB;YACxD,OAAO,EAAE,0BAA0B;YACnC,aAAa,EAAE,0BAA0B;SAC1C,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;QAC1B,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,MAAM,+BAA+B,GAAsC,IAAI,GAAG,EAAE,CAAC;YACrF,KAAK,MAAM,SAAS,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;gBAC1C,+BAA+B,CAAC,GAAG,CAAC,SAAS,CAAC,QAAQ,EAAE,SAAiC,CAAC,CAAC;gBAC3F,IAAI,SAAS,CAAC,SAAS,EAAE,CAAC;oBACxB,+BAA+B,CAAC,GAAG,CAAC,SAAS,CAAC,SAAS,EAAE,SAAiC,CAAC,CAAC;gBAC9F,CAAC;YACH,CAAC;YACD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,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AAC;YAED,IAAI,8BAA8B,aAA9B,8BAA8B,uBAA9B,8BAA8B,CAAE,WAAW,EAAE,CAAC;gBAChD,qBAAqB,GAAG,MAAM,8BAA8B,CAAC,WAAW,EAAE,CAAC;YAC7E,CAAC;QACH,CAAC;QAED,OAAO,qBAAqB,CAAC;IAC/B,CAAC;IAEO,yBAAyB,CAAC,MAAgB;QAChD,MAAM,gBAAgB,GAAsC,IAAI,CAAC,iBAAiB,CAAC;QACnF,IAAI,KAAK,GAAW,CAAC,CAAC;QAEtB,KAAK,EAAE,KAAK,IAAI,CAAC,GAAW,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACtD,KAAK,MAAM,eAAe,IAAI,gBAAgB,CAAC,MAAM,EAAE,EAAE,CAAC;gBACxD,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,eAAe,CAAC,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,eAAe,CAAC,SAAS,EAAE,CAAC;oBACtF,MAAM,KAAK,CAAC;gBACd,CAAC;YACH,CAAC;YACD,KAAK,EAAE,CAAC;QACV,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAEO,CAAC,wBAAwB,CAC/B,qBAA4C,EAC5C,SAAiB;QAEjB,KAAK,MAAM,oBAAoB,IAAI,qBAAqB,EAAE,CAAC;YACzD,IAAI,oBAAoB,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;gBAClD,MAAM,oBAAoB,CAAC;YAC7B,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA9MD,8CA8MC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport stringArgv from 'string-argv';\n\nimport type { IRequiredCommandLineIntegerParameter } from '../parameters/CommandLineIntegerParameter';\nimport type { IRequiredCommandLineStringParameter } from '../parameters/CommandLineStringParameter';\nimport {\n CommandLineParameterKind,\n type CommandLineParameterBase,\n CommandLineParameterWithArgument,\n type CommandLineParameter\n} from '../parameters/BaseClasses';\nimport { CommandLineChoiceParameter } from '../parameters/CommandLineChoiceParameter';\nimport { CommandLineAction } from './CommandLineAction';\nimport { CommandLineConstants } from '../Constants';\n\nconst DEFAULT_WORD_TO_AUTOCOMPLETE: string = '';\nconst DEFAULT_POSITION: number = 0;\n\nexport class TabCompleteAction extends CommandLineAction {\n private readonly _wordToCompleteParameter: IRequiredCommandLineStringParameter;\n private readonly _positionParameter: IRequiredCommandLineIntegerParameter;\n private readonly _actions: Map<string, Map<string, CommandLineParameter>>;\n private readonly _globalParameters: Map<string, CommandLineParameter>;\n\n public constructor(\n actions: ReadonlyArray<CommandLineAction>,\n globalParameters: ReadonlyArray<CommandLineParameterBase>\n ) {\n super({\n actionName: CommandLineConstants.TabCompletionActionName,\n summary: 'Provides tab completion.',\n documentation: 'Provides tab completion.'\n });\n\n this._actions = new Map();\n for (const action of actions) {\n const parameterNameToParameterInfoMap: Map<string, CommandLineParameter> = new Map();\n for (const parameter of action.parameters) {\n parameterNameToParameterInfoMap.set(parameter.longName, parameter as CommandLineParameter);\n if (parameter.shortName) {\n parameterNameToParameterInfoMap.set(parameter.shortName, parameter as CommandLineParameter);\n }\n }\n this._actions.set(action.actionName, parameterNameToParameterInfoMap);\n }\n\n this._globalParameters = new Map<string, CommandLineParameter>();\n for (const parameter of globalParameters) {\n this._globalParameters.set(parameter.longName, parameter as CommandLineParameter);\n if (parameter.shortName) {\n this._globalParameters.set(parameter.shortName, parameter as CommandLineParameter);\n }\n }\n\n this._wordToCompleteParameter = this.defineStringParameter({\n parameterLongName: '--word',\n argumentName: 'WORD',\n description: `The word to complete.`,\n defaultValue: DEFAULT_WORD_TO_AUTOCOMPLETE\n });\n\n this._positionParameter = this.defineIntegerParameter({\n parameterLongName: '--position',\n argumentName: 'INDEX',\n description: `The position in the word to be completed.`,\n defaultValue: DEFAULT_POSITION\n });\n }\n\n protected async onExecute(): Promise<void> {\n const commandLine: string = this._wordToCompleteParameter.value;\n const caretPosition: number = this._positionParameter.value || commandLine.length;\n\n for await (const value of this.getCompletionsAsync(commandLine, caretPosition)) {\n // eslint-disable-next-line no-console\n console.log(value);\n }\n }\n\n public async *getCompletionsAsync(\n commandLine: string,\n caretPosition: number = commandLine.length\n ): AsyncIterable<string> {\n const actions: Map<string, Map<string, CommandLineParameter>> = this._actions;\n\n if (!commandLine || !caretPosition) {\n yield* this._getAllActions();\n return;\n }\n\n const tokens: string[] = Array.from(this.tokenizeCommandLine(commandLine));\n\n // offset arguments by the number of global params in the input\n const globalParameterOffset: number = this._getGlobalParameterOffset(tokens);\n\n if (tokens.length < 2 + globalParameterOffset) {\n yield* this._getAllActions();\n return;\n }\n\n const lastToken: string = tokens[tokens.length - 1];\n const secondLastToken: string = tokens[tokens.length - 2];\n\n const lastCharacterIsWhitespace: boolean = !commandLine.slice(-1).trim();\n const completePartialWord: boolean = caretPosition === commandLine.length && !lastCharacterIsWhitespace;\n\n if (completePartialWord && tokens.length === 2 + globalParameterOffset) {\n for (const actionName of actions.keys()) {\n if (actionName.indexOf(tokens[1 + globalParameterOffset]) === 0) {\n yield actionName;\n }\n }\n } else {\n for (const actionName of actions.keys()) {\n if (actionName === tokens[1 + globalParameterOffset]) {\n const parameterNameMap: Map<string, CommandLineParameter> = actions.get(actionName)!;\n\n const parameterNames: string[] = Array.from(parameterNameMap.keys());\n\n if (completePartialWord) {\n for (const parameterName of parameterNames) {\n if (parameterName === secondLastToken) {\n const values: ReadonlyArray<string> = await this._getParameterValueCompletionsAsync(\n parameterNameMap.get(parameterName)!\n );\n if (values.length > 0) {\n yield* this._completeParameterValues(values, lastToken);\n return;\n }\n }\n }\n yield* this._completeParameterValues(parameterNames, lastToken);\n } else {\n for (const parameterName of parameterNames) {\n if (parameterName === lastToken) {\n const values: ReadonlyArray<string> = await this._getParameterValueCompletionsAsync(\n parameterNameMap.get(parameterName)!\n );\n if (values.length > 0) {\n yield* values;\n return;\n }\n }\n }\n for (const parameterName of parameterNames) {\n if (\n parameterName === lastToken &&\n parameterNameMap.get(parameterName)!.kind !== CommandLineParameterKind.Flag\n ) {\n // The parameter is expecting a value, so don't suggest parameter names again\n return;\n }\n }\n\n yield* parameterNames;\n }\n\n break;\n }\n }\n }\n }\n\n private *_getAllActions(): IterableIterator<string> {\n yield* this._actions.keys();\n yield* this._globalParameters.keys();\n }\n\n public tokenizeCommandLine(commandLine: string): string[] {\n return stringArgv(commandLine);\n }\n\n private async _getParameterValueCompletionsAsync(\n parameter: CommandLineParameter\n ): Promise<ReadonlyArray<string>> {\n let choiceParameterValues: ReadonlyArray<string> = [];\n if (parameter.kind === CommandLineParameterKind.Choice) {\n choiceParameterValues = parameter.alternatives;\n } else if (parameter.kind !== CommandLineParameterKind.Flag) {\n let parameterWithArgumentOrChoices:\n | CommandLineParameterWithArgument\n | CommandLineChoiceParameter\n | undefined = undefined;\n if (\n parameter instanceof CommandLineParameterWithArgument ||\n parameter instanceof CommandLineChoiceParameter\n ) {\n parameterWithArgumentOrChoices = parameter;\n }\n\n if (parameterWithArgumentOrChoices?.completions) {\n choiceParameterValues = await parameterWithArgumentOrChoices.completions();\n }\n }\n\n return choiceParameterValues;\n }\n\n private _getGlobalParameterOffset(tokens: string[]): number {\n const globalParameters: Map<string, CommandLineParameter> = this._globalParameters;\n let count: number = 0;\n\n outer: for (let i: number = 1; i < tokens.length; i++) {\n for (const globalParameter of globalParameters.values()) {\n if (tokens[i] !== globalParameter.longName && tokens[i] !== globalParameter.shortName) {\n break outer;\n }\n }\n count++;\n }\n\n return count;\n }\n\n private *_completeParameterValues(\n choiceParameterValues: ReadonlyArray<string>,\n lastToken: string\n ): IterableIterator<string> {\n for (const choiceParameterValue of choiceParameterValues) {\n if (choiceParameterValue.indexOf(lastToken) === 0) {\n yield choiceParameterValue;\n }\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/ts-command-line",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.22.1",
|
|
4
4
|
"description": "An object-oriented command-line parser for TypeScript",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
"@types/argparse": "1.0.38",
|
|
15
15
|
"argparse": "~1.0.9",
|
|
16
16
|
"string-argv": "~0.3.1",
|
|
17
|
-
"@rushstack/terminal": "0.
|
|
17
|
+
"@rushstack/terminal": "0.13.1"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@rushstack/heft": "0.66.
|
|
21
|
-
"@rushstack/heft-node-rig": "2.6.
|
|
20
|
+
"@rushstack/heft": "0.66.17",
|
|
21
|
+
"@rushstack/heft-node-rig": "2.6.15",
|
|
22
22
|
"@types/heft-jest": "1.0.1",
|
|
23
23
|
"@types/node": "18.17.15",
|
|
24
24
|
"local-eslint-config": "1.0.0"
|