Skip to content

Commit 41ce551

Browse files
Copilotdmichon-msfticlanton
authored
Remove abstract modifier from CommandLineParser and eliminate redundant _actions field (#5587)
* Initial plan * Remove abstract modifier from CommandLineParser and remove redundant _actions array Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com> * Add rush change file for ts-command-line patch Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com> * Revise change file Co-authored-by: Ian Clanton-Thuon <iclanton@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com> Co-authored-by: David Michon <dmichon@microsoft.com> Co-authored-by: Ian Clanton-Thuon <iclanton@users.noreply.github.com>
1 parent 190aac0 commit 41ce551

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/ts-command-line",
5+
"comment": "Remove `abstract` modifier from `CommandLineParser` class to allow direct instantiation. Remove redundant `_actions` array field.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/ts-command-line"
10+
}

common/reviews/api/ts-command-line.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export abstract class CommandLineParameterWithArgument extends CommandLineParame
249249
}
250250

251251
// @public
252-
export abstract class CommandLineParser extends CommandLineParameterProvider {
252+
export class CommandLineParser extends CommandLineParameterProvider {
253253
constructor(options: ICommandLineParserOptions);
254254
get actions(): ReadonlyArray<CommandLineAction>;
255255
addAction(action: CommandLineAction): void;

libraries/ts-command-line/src/providers/CommandLineParser.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ export interface ICommandLineParserOptions {
4848
* The "argparse" library is a relatively advanced command-line parser with features such
4949
* as word-wrapping and intelligible error messages (that are lacking in other similar
5050
* libraries such as commander, yargs, and nomnom). Unfortunately, its ruby-inspired API
51-
* is awkward to use. The abstract base classes CommandLineParser and CommandLineAction
51+
* is awkward to use. The base classes CommandLineParser and CommandLineAction
5252
* provide a wrapper for "argparse" that makes defining and consuming arguments quick
5353
* and simple, and enforces that appropriate documentation is provided for each parameter.
5454
*
5555
* @public
5656
*/
57-
export abstract class CommandLineParser extends CommandLineParameterProvider {
57+
export class CommandLineParser extends CommandLineParameterProvider {
5858
/**
5959
* Reports which CommandLineAction was specified on the command line.
6060
* @remarks
@@ -65,7 +65,6 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
6565
private readonly _argumentParser: argparse.ArgumentParser;
6666
private _actionsSubParser: argparse.SubParser | undefined;
6767
private readonly _options: ICommandLineParserOptions;
68-
private readonly _actions: CommandLineAction[];
6968
private readonly _actionsByName: Map<string, CommandLineAction>;
7069
private _executed: boolean = false;
7170
private _tabCompleteActionWasAdded: boolean = false;
@@ -74,7 +73,6 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
7473
super();
7574

7675
this._options = options;
77-
this._actions = [];
7876
this._actionsByName = new Map<string, CommandLineAction>();
7977

8078
const { toolFilename, toolDescription, toolEpilog } = options;
@@ -95,7 +93,7 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
9593
* Returns the list of actions that were defined for this CommandLineParser object.
9694
*/
9795
public get actions(): ReadonlyArray<CommandLineAction> {
98-
return this._actions;
96+
return Array.from(this._actionsByName.values());
9997
}
10098

10199
/**
@@ -110,7 +108,6 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
110108
}
111109

112110
action._buildParser(this._actionsSubParser);
113-
this._actions.push(action);
114111
this._actionsByName.set(action.actionName, action);
115112
}
116113

@@ -314,7 +311,7 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
314311
...state,
315312
parentParameterNames: updatedParentParameterNames
316313
};
317-
for (const action of this._actions) {
314+
for (const action of this._actionsByName.values()) {
318315
action._registerDefinedParameters(parentState);
319316
}
320317
}

libraries/ts-command-line/src/test/CommandLineParser.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,22 @@ describe(CommandLineParser.name, () => {
5858
const action: TestAction = commandLineParser.selectedAction as TestAction;
5959
expect(action.done).toBe(true);
6060
});
61+
62+
it('can be instantiated directly without subclassing', () => {
63+
// This test verifies that CommandLineParser can be instantiated directly,
64+
// which is useful for test scenarios
65+
const commandLineParser = new CommandLineParser({
66+
toolFilename: 'test-tool',
67+
toolDescription: 'A test tool'
68+
});
69+
70+
expect(commandLineParser).toBeDefined();
71+
expect(commandLineParser.actions).toEqual([]);
72+
73+
// Verify that addAction can be called on the direct instance
74+
const action = new TestAction();
75+
commandLineParser.addAction(action);
76+
expect(commandLineParser.actions).toHaveLength(1);
77+
expect(commandLineParser.actions[0]).toBe(action);
78+
});
6179
});

0 commit comments

Comments
 (0)