@rushstack/ts-command-line 5.1.7 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.json CHANGED
@@ -1,6 +1,18 @@
1
1
  {
2
2
  "name": "@rushstack/ts-command-line",
3
3
  "entries": [
4
+ {
5
+ "version": "5.2.0",
6
+ "tag": "@rushstack/ts-command-line_v5.2.0",
7
+ "date": "Wed, 04 Feb 2026 16:13:27 GMT",
8
+ "comments": {
9
+ "minor": [
10
+ {
11
+ "comment": "Remove `abstract` modifier from `CommandLineParser` class to allow direct instantiation. Remove redundant `_actions` array field."
12
+ }
13
+ ]
14
+ }
15
+ },
4
16
  {
5
17
  "version": "5.1.7",
6
18
  "tag": "@rushstack/ts-command-line_v5.1.7",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Change Log - @rushstack/ts-command-line
2
2
 
3
- This log was last generated on Wed, 07 Jan 2026 01:12:25 GMT and should not be manually modified.
3
+ This log was last generated on Wed, 04 Feb 2026 16:13:27 GMT and should not be manually modified.
4
+
5
+ ## 5.2.0
6
+ Wed, 04 Feb 2026 16:13:27 GMT
7
+
8
+ ### Minor changes
9
+
10
+ - Remove `abstract` modifier from `CommandLineParser` class to allow direct instantiation. Remove redundant `_actions` array field.
4
11
 
5
12
  ## 5.1.7
6
13
  Wed, 07 Jan 2026 01:12:25 GMT
@@ -578,13 +578,13 @@ export declare abstract class CommandLineParameterWithArgument extends CommandLi
578
578
  * The "argparse" library is a relatively advanced command-line parser with features such
579
579
  * as word-wrapping and intelligible error messages (that are lacking in other similar
580
580
  * libraries such as commander, yargs, and nomnom). Unfortunately, its ruby-inspired API
581
- * is awkward to use. The abstract base classes CommandLineParser and CommandLineAction
581
+ * is awkward to use. The base classes CommandLineParser and CommandLineAction
582
582
  * provide a wrapper for "argparse" that makes defining and consuming arguments quick
583
583
  * and simple, and enforces that appropriate documentation is provided for each parameter.
584
584
  *
585
585
  * @public
586
586
  */
587
- export declare abstract class CommandLineParser extends CommandLineParameterProvider {
587
+ export declare class CommandLineParser extends CommandLineParameterProvider {
588
588
  /**
589
589
  * Reports which CommandLineAction was specified on the command line.
590
590
  * @remarks
@@ -594,7 +594,6 @@ export declare abstract class CommandLineParser extends CommandLineParameterProv
594
594
  private readonly _argumentParser;
595
595
  private _actionsSubParser;
596
596
  private readonly _options;
597
- private readonly _actions;
598
597
  private readonly _actionsByName;
599
598
  private _executed;
600
599
  private _tabCompleteActionWasAdded;
@@ -28,13 +28,13 @@ export interface ICommandLineParserOptions {
28
28
  * The "argparse" library is a relatively advanced command-line parser with features such
29
29
  * as word-wrapping and intelligible error messages (that are lacking in other similar
30
30
  * libraries such as commander, yargs, and nomnom). Unfortunately, its ruby-inspired API
31
- * is awkward to use. The abstract base classes CommandLineParser and CommandLineAction
31
+ * is awkward to use. The base classes CommandLineParser and CommandLineAction
32
32
  * provide a wrapper for "argparse" that makes defining and consuming arguments quick
33
33
  * and simple, and enforces that appropriate documentation is provided for each parameter.
34
34
  *
35
35
  * @public
36
36
  */
37
- export declare abstract class CommandLineParser extends CommandLineParameterProvider {
37
+ export declare class CommandLineParser extends CommandLineParameterProvider {
38
38
  /**
39
39
  * Reports which CommandLineAction was specified on the command line.
40
40
  * @remarks
@@ -44,7 +44,6 @@ export declare abstract class CommandLineParser extends CommandLineParameterProv
44
44
  private readonly _argumentParser;
45
45
  private _actionsSubParser;
46
46
  private readonly _options;
47
- private readonly _actions;
48
47
  private readonly _actionsByName;
49
48
  private _executed;
50
49
  private _tabCompleteActionWasAdded;
@@ -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;AAI1C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAE7D,OAAO,EACL,4BAA4B,EAC5B,KAAK,+BAA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IAQvD;;;OAGG;IACI,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,iBAAiB,GAAG,SAAS;IAItE;;;;;;;;;;;;;;;;OAgBG;IACU,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IA+C5D;;;OAGG;IACU,gCAAgC,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAkG7E,gBAAgB;IACT,0BAA0B,CAAC,KAAK,EAAE,+BAA+B,GAAG,IAAI;IAkB/E,OAAO,CAAC,oBAAoB;IAO5B;;;OAGG;cACgB,kBAAkB,IAAI,QAAQ,CAAC,cAAc;IAIhE;;;OAGG;cACa,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;CAKhD"}
@@ -13,7 +13,7 @@ const escapeSprintf_1 = require("../escapeSprintf");
13
13
  * The "argparse" library is a relatively advanced command-line parser with features such
14
14
  * as word-wrapping and intelligible error messages (that are lacking in other similar
15
15
  * libraries such as commander, yargs, and nomnom). Unfortunately, its ruby-inspired API
16
- * is awkward to use. The abstract base classes CommandLineParser and CommandLineAction
16
+ * is awkward to use. The base classes CommandLineParser and CommandLineAction
17
17
  * provide a wrapper for "argparse" that makes defining and consuming arguments quick
18
18
  * and simple, and enforces that appropriate documentation is provided for each parameter.
19
19
  *
@@ -25,7 +25,6 @@ class CommandLineParser extends CommandLineParameterProvider_1.CommandLineParame
25
25
  this._executed = false;
26
26
  this._tabCompleteActionWasAdded = false;
27
27
  this._options = options;
28
- this._actions = [];
29
28
  this._actionsByName = new Map();
30
29
  const { toolFilename, toolDescription, toolEpilog } = options;
31
30
  this._argumentParser = new CommandLineParserExitError_1.CustomArgumentParser({
@@ -39,7 +38,7 @@ class CommandLineParser extends CommandLineParameterProvider_1.CommandLineParame
39
38
  * Returns the list of actions that were defined for this CommandLineParser object.
40
39
  */
41
40
  get actions() {
42
- return this._actions;
41
+ return Array.from(this._actionsByName.values());
43
42
  }
44
43
  /**
45
44
  * Defines a new action that can be used with the CommandLineParser instance.
@@ -52,7 +51,6 @@ class CommandLineParser extends CommandLineParameterProvider_1.CommandLineParame
52
51
  });
53
52
  }
54
53
  action._buildParser(this._actionsSubParser);
55
- this._actions.push(action);
56
54
  this._actionsByName.set(action.actionName, action);
57
55
  }
58
56
  /**
@@ -237,7 +235,7 @@ class CommandLineParser extends CommandLineParameterProvider_1.CommandLineParame
237
235
  ...state,
238
236
  parentParameterNames: updatedParentParameterNames
239
237
  };
240
- for (const action of this._actions) {
238
+ for (const action of this._actionsByName.values()) {
241
239
  action._registerDefinedParameters(parentState);
242
240
  }
243
241
  }
@@ -1 +1 @@
1
- {"version":3,"file":"CommandLineParser.js","sourceRoot":"","sources":["../../src/providers/CommandLineParser.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAI3D,kDAA+C;AAI/C,iFAIwC;AACxC,6EAAgG;AAChG,+DAA0D;AAC1D,kDAAqE;AACrE,oDAAiD;AA6BjD;;;;;;;;;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,MAAM,EAAE,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;QAE9D,IAAI,CAAC,eAAe,GAAG,IAAI,iDAAoB,CAAC;YAC9C,OAAO,EAAE,IAAI;YACb,IAAI,EAAE,YAAY;YAClB,WAAW,EAAE,IAAA,6BAAa,EAAC,eAAe,CAAC;YAC3C,MAAM,EAAE,mBAAQ,CAAC,IAAI,CACnB,IAAA,6BAAa,EACX,UAAU,aAA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AG,IAAI,CAAC,oCAAoC,CAAC,IAAI,EAAE;SACpD,CAAC,CAAC;QAEH,MAAM,WAAW,GAAoC;YACnD,GAAG,KAAK;YACR,oBAAoB,EAAE,2BAA2B;SAClD,CAAC;QACF,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnC,MAAM,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;IACgB,kBAAkB;QACnC,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACO,KAAK,CAAC,cAAc;QAC5B,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxB,MAAM,IAAI,CAAC,cAAc,CAAC,aAAa,EAAE,CAAC;QAC5C,CAAC;IACH,CAAC;CACF;AAjSD,8CAiSC","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';\n\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';\nimport { escapeSprintf } from '../escapeSprintf';\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 const { toolFilename, toolDescription, toolEpilog } = options;\n\n this._argumentParser = new CustomArgumentParser({\n addHelp: true,\n prog: toolFilename,\n description: escapeSprintf(toolDescription),\n epilog: Colorize.bold(\n escapeSprintf(\n toolEpilog ?? `For detailed help about a specific command, use: ${toolFilename} <command> -h`\n )\n )\n });\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 * 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.onExecuteAsync();\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 /** @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 override _getArgumentParser(): argparse.ArgumentParser {\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 onExecuteAsync(): Promise<void> {\n if (this.selectedAction) {\n await this.selectedAction._executeAsync();\n }\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xG,CAAC;IACH,CAAC;IAED;;;OAGG;IACgB,kBAAkB;QACnC,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACO,KAAK,CAAC,cAAc;QAC5B,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxB,MAAM,IAAI,CAAC,cAAc,CAAC,aAAa,EAAE,CAAC;QAC5C,CAAC;IACH,CAAC;CACF;AA9RD,8CA8RC","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';\n\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';\nimport { escapeSprintf } from '../escapeSprintf';\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 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 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 _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._actionsByName = new Map<string, CommandLineAction>();\n\n const { toolFilename, toolDescription, toolEpilog } = options;\n\n this._argumentParser = new CustomArgumentParser({\n addHelp: true,\n prog: toolFilename,\n description: escapeSprintf(toolDescription),\n epilog: Colorize.bold(\n escapeSprintf(\n toolEpilog ?? `For detailed help about a specific command, use: ${toolFilename} <command> -h`\n )\n )\n });\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 Array.from(this._actionsByName.values());\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._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 * 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.onExecuteAsync();\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 /** @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._actionsByName.values()) {\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 override _getArgumentParser(): argparse.ArgumentParser {\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 onExecuteAsync(): Promise<void> {\n if (this.selectedAction) {\n await this.selectedAction._executeAsync();\n }\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rushstack/ts-command-line",
3
- "version": "5.1.7",
3
+ "version": "5.2.0",
4
4
  "description": "An object-oriented command-line parser for TypeScript",
5
5
  "repository": {
6
6
  "type": "git",