Skip to content

Commit ae0b10b

Browse files
committed
- Make the define___Parameter() members public
- Add a unit test for DynamicCommandLineParser
1 parent c5b0cde commit ae0b10b

5 files changed

Lines changed: 55 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ class CommandLineParameterProvider {
5050
protected _argumentParser: argparse.ArgumentParser;
5151
// @internal (undocumented)
5252
protected _processParsedData(data: ICommandLineParserData): void;
53-
protected defineChoiceParameter(definition: ICommandLineChoiceDefinition): CommandLineChoiceParameter;
54-
protected defineFlagParameter(definition: ICommandLineFlagDefinition): CommandLineFlagParameter;
55-
protected defineIntegerParameter(definition: ICommandLineIntegerDefinition): CommandLineIntegerParameter;
56-
protected defineStringListParameter(definition: ICommandLineStringListDefinition): CommandLineStringListParameter;
57-
protected defineStringParameter(definition: ICommandLineStringDefinition): CommandLineStringParameter;
53+
defineChoiceParameter(definition: ICommandLineChoiceDefinition): CommandLineChoiceParameter;
54+
defineFlagParameter(definition: ICommandLineFlagDefinition): CommandLineFlagParameter;
55+
defineIntegerParameter(definition: ICommandLineIntegerDefinition): CommandLineIntegerParameter;
56+
defineStringListParameter(definition: ICommandLineStringListDefinition): CommandLineStringListParameter;
57+
defineStringParameter(definition: ICommandLineStringDefinition): CommandLineStringParameter;
5858
protected abstract onDefineParameters(): void;
5959
}
6060

libraries/ts-command-line/src/CommandLineParameterProvider.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,14 @@ export abstract class CommandLineParameterProvider {
4949
this._keys = new Map<string, string>();
5050
}
5151

52-
/**
53-
* The child class should implement this hook to define its command-line parameters,
54-
* e.g. by calling defineFlagParameter().
55-
*/
56-
protected abstract onDefineParameters(): void;
57-
5852
/**
5953
* Defines a command-line switch whose boolean value is true if the switch is provided,
6054
* and false otherwise.
6155
*
6256
* @remarks
6357
* Example: example-tool --debug
6458
*/
65-
protected defineFlagParameter(definition: ICommandLineFlagDefinition): CommandLineFlagParameter {
59+
public defineFlagParameter(definition: ICommandLineFlagDefinition): CommandLineFlagParameter {
6660
return this._createParameter(definition, {
6761
action: 'storeTrue'
6862
}) as CommandLineFlagParameter;
@@ -74,7 +68,7 @@ export abstract class CommandLineParameterProvider {
7468
* @remarks
7569
* Example: example-tool --message "Hello, world!"
7670
*/
77-
protected defineStringParameter(definition: ICommandLineStringDefinition): CommandLineStringParameter {
71+
public defineStringParameter(definition: ICommandLineStringDefinition): CommandLineStringParameter {
7872
return this._createParameter(definition, undefined, definition.key) as CommandLineStringParameter;
7973
}
8074

@@ -84,7 +78,7 @@ export abstract class CommandLineParameterProvider {
8478
* @remarks
8579
* Example: example-tool --add file1.txt --add file2.txt --add file3.txt
8680
*/
87-
protected defineStringListParameter(definition: ICommandLineStringListDefinition): CommandLineStringListParameter {
81+
public defineStringListParameter(definition: ICommandLineStringListDefinition): CommandLineStringListParameter {
8882
return this._createParameter(definition, {
8983
action: 'append'
9084
}, definition.key) as CommandLineStringListParameter;
@@ -96,7 +90,7 @@ export abstract class CommandLineParameterProvider {
9690
* @remarks
9791
* Example: example-tool l --max-attempts 5
9892
*/
99-
protected defineIntegerParameter(definition: ICommandLineIntegerDefinition): CommandLineIntegerParameter {
93+
public defineIntegerParameter(definition: ICommandLineIntegerDefinition): CommandLineIntegerParameter {
10094
return this._createParameter(definition, {
10195
type: 'int'
10296
}, definition.key) as CommandLineIntegerParameter;
@@ -109,7 +103,7 @@ export abstract class CommandLineParameterProvider {
109103
* @remarks
110104
* Example: example-tool --log-level warn
111105
*/
112-
protected defineChoiceParameter(definition: ICommandLineChoiceDefinition): CommandLineChoiceParameter {
106+
public defineChoiceParameter(definition: ICommandLineChoiceDefinition): CommandLineChoiceParameter {
113107
if (!definition.options) {
114108
throw new Error(`When defining an option parameter, the options array must be defined.`);
115109
}
@@ -123,6 +117,12 @@ export abstract class CommandLineParameterProvider {
123117
}) as CommandLineChoiceParameter;
124118
}
125119

120+
/**
121+
* The child class should implement this hook to define its command-line parameters,
122+
* e.g. by calling defineFlagParameter().
123+
*/
124+
protected abstract onDefineParameters(): void;
125+
126126
/** @internal */
127127
protected _processParsedData(data: ICommandLineParserData): void {
128128
// Fill in the values for the parameters

libraries/ts-command-line/src/DynamicCommandLineAction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { CommandLineAction } from './CommandLineAction';
77
* @public
88
*/
99
export class DynamicCommandLineAction extends CommandLineAction {
10-
1110
protected onDefineParameters(): void { // abstract
11+
// (handled by the external code)
1212
}
1313

1414
protected onExecute(): Promise<void> { // abstract
15+
// (handled by the external code)
1516
return Promise.resolve();
1617
}
1718
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class TestCommandLine extends CommandLineParser {
3737
toolFilename: 'example',
3838
toolDescription: 'An example project'
3939
});
40+
41+
this.addAction(new TestAction());
4042
}
4143

4244
protected onDefineParameters(): void {
@@ -48,11 +50,12 @@ describe('CommandLineParser tests', () => {
4850

4951
it('simple case', () => {
5052
const commandLineParser: TestCommandLine = new TestCommandLine();
51-
const action: TestAction = new TestAction();
52-
commandLineParser.addAction(action);
5353

5454
return commandLineParser.execute(['do-job', '--flag']).then(() => {
5555
expect(commandLineParser.selectedAction).toBeDefined();
56+
expect(commandLineParser.selectedAction.options.actionVerb).toEqual('do-job');
57+
58+
const action: TestAction = commandLineParser.selectedAction as TestAction;
5659
expect(action.done).toBe(true);
5760
});
5861
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { DynamicCommandLineParser } from '../DynamicCommandLineParser';
5+
import { DynamicCommandLineAction } from '../DynamicCommandLineAction';
6+
7+
describe('DynamicCommandLineParser tests', () => {
8+
9+
it('simple case', () => {
10+
const commandLineParser: DynamicCommandLineParser = new DynamicCommandLineParser(
11+
{
12+
toolFilename: 'example',
13+
toolDescription: 'An example project'
14+
}
15+
);
16+
17+
const action: DynamicCommandLineAction = new DynamicCommandLineAction({
18+
actionVerb: 'do-job',
19+
summary: 'does the job',
20+
documentation: 'a longer description'
21+
});
22+
commandLineParser.addAction(action);
23+
action.defineFlagParameter({
24+
parameterLongName: '--flag',
25+
description: 'The flag'
26+
});
27+
28+
return commandLineParser.execute(['do-job', '--flag']).then(() => {
29+
expect(commandLineParser.selectedAction).toEqual(action);
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)