Skip to content

Commit fdc7bd8

Browse files
committed
Add CommandLineParameter.appendToArgList() and CommandLineParser.ts()
1 parent b638b8f commit fdc7bd8

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class CommandLineChoiceParameter extends CommandLineParameter {
3333
// @internal
3434
_setValue(data: any): void;
3535
readonly alternatives: ReadonlyArray<string>;
36+
// @override
37+
appendToArgList(argList: string[]): void;
3638
readonly defaultValue: string | undefined;
3739
readonly kind: CommandLineParameterKind;
3840
readonly value: string | undefined;
@@ -44,6 +46,8 @@ class CommandLineFlagParameter extends CommandLineParameter {
4446
constructor(definition: ICommandLineFlagDefinition);
4547
// @internal
4648
_setValue(data: any): void;
49+
// @override
50+
appendToArgList(argList: string[]): void;
4751
readonly kind: CommandLineParameterKind;
4852
readonly value: boolean;
4953
}
@@ -56,6 +60,8 @@ class CommandLineIntegerParameter extends CommandLineParameterWithArgument {
5660
_getSupplementaryNotes(supplementaryNotes: string[]): void;
5761
// @internal
5862
_setValue(data: any): void;
63+
// @override
64+
appendToArgList(argList: string[]): void;
5965
readonly defaultValue: number | undefined;
6066
readonly kind: CommandLineParameterKind;
6167
readonly value: number | undefined;
@@ -71,6 +77,7 @@ class CommandLineParameter {
7177
_parserKey: string;
7278
// @internal
7379
abstract _setValue(data: any): void;
80+
abstract appendToArgList(argList: string[]): void;
7481
readonly description: string;
7582
readonly environmentVariable: string | undefined;
7683
readonly kind: CommandLineParameterKind;
@@ -135,6 +142,7 @@ class CommandLineParser extends CommandLineParameterProvider {
135142
selectedAction: CommandLineAction | undefined;
136143
readonly toolDescription: string;
137144
readonly toolFilename: string;
145+
tryGetAction(actionName: string): CommandLineAction | undefined;
138146
}
139147

140148
// @public
@@ -143,6 +151,8 @@ class CommandLineStringListParameter extends CommandLineParameterWithArgument {
143151
constructor(definition: ICommandLineStringListDefinition);
144152
// @internal
145153
_setValue(data: any): void;
154+
// @override
155+
appendToArgList(argList: string[]): void;
146156
readonly kind: CommandLineParameterKind;
147157
readonly values: ReadonlyArray<string>;
148158
}
@@ -155,6 +165,8 @@ class CommandLineStringParameter extends CommandLineParameterWithArgument {
155165
_getSupplementaryNotes(supplementaryNotes: string[]): void;
156166
// @internal
157167
_setValue(data: any): void;
168+
// @override
169+
appendToArgList(argList: string[]): void;
158170
readonly defaultValue: string | undefined;
159171
readonly kind: CommandLineParameterKind;
160172
readonly value: string | undefined;

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ export abstract class CommandLineParameter {
121121
*/
122122
public abstract get kind(): CommandLineParameterKind;
123123

124+
/**
125+
* Append the parsed values to the provided string array.
126+
* @remarks
127+
* Sometimes a command line parameter is not used directly, but instead gets passed through to another
128+
* tool that will use it. For example if our parameter comes in as "--max-count 3", then we might want to
129+
* call `child_process.spawn()` and append ["--max-count", "3"] to the args array for that tool.
130+
* appendToArgList() appends zero or more strings to the provided array, based on the input command-line
131+
* that we parsed.
132+
*
133+
* If the parameter was omitted from our command-line and has no default value, then
134+
* nothing will be appended. If the short name was used, the long name will be appended instead.
135+
* @param argList - the parsed strings will be appended to this string array
136+
*/
137+
public abstract appendToArgList(argList: string[]): void;
138+
124139
/**
125140
* Internal usage only. Used to report unexpected output from the argparse library.
126141
*/
@@ -269,6 +284,14 @@ export class CommandLineChoiceParameter extends CommandLineParameter {
269284
public get value(): string | undefined {
270285
return this._value;
271286
}
287+
288+
/** {@inheritdoc CommandLineParameter.appendToArgList} @override */
289+
public appendToArgList(argList: string[]): void {
290+
if (this.value !== undefined) {
291+
argList.push(this.longName);
292+
argList.push(this.value);
293+
}
294+
}
272295
}
273296

274297
/**
@@ -328,6 +351,13 @@ export class CommandLineFlagParameter extends CommandLineParameter {
328351
public get value(): boolean {
329352
return this._value;
330353
}
354+
355+
/** {@inheritdoc CommandLineParameter.appendToArgList} @override */
356+
public appendToArgList(argList: string[]): void {
357+
if (this.value) {
358+
argList.push(this.longName);
359+
}
360+
}
331361
}
332362

333363
/**
@@ -409,6 +439,14 @@ export class CommandLineIntegerParameter extends CommandLineParameterWithArgumen
409439
public get value(): number | undefined {
410440
return this._value;
411441
}
442+
443+
/** {@inheritdoc CommandLineParameter.appendToArgList} @override */
444+
public appendToArgList(argList: string[]): void {
445+
if (this.value !== undefined) {
446+
argList.push(this.longName);
447+
argList.push(this.value.toString());
448+
}
449+
}
412450
}
413451

414452
/**
@@ -490,6 +528,15 @@ export class CommandLineStringParameter extends CommandLineParameterWithArgument
490528
public get value(): string | undefined {
491529
return this._value;
492530
}
531+
532+
/** {@inheritdoc CommandLineParameter.appendToArgList} @override */
533+
public appendToArgList(argList: string[]): void {
534+
if (this.value !== undefined) {
535+
argList.push(this.longName);
536+
argList.push(this.value);
537+
}
538+
}
539+
493540
}
494541

495542
/**
@@ -560,4 +607,14 @@ export class CommandLineStringListParameter extends CommandLineParameterWithArgu
560607
public get values(): ReadonlyArray<string> {
561608
return this._values;
562609
}
610+
611+
/** {@inheritdoc CommandLineParameter.appendToArgList} @override */
612+
public appendToArgList(argList: string[]): void {
613+
if (this.values.length > 0) {
614+
for (const value of this.values) {
615+
argList.push(this.longName);
616+
argList.push(value);
617+
}
618+
}
619+
}
563620
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,21 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
130130
* an exception is thrown.
131131
*/
132132
public getAction(actionName: string): CommandLineAction {
133-
const action: CommandLineAction | undefined = this._actionsByName.get(actionName);
133+
const action: CommandLineAction | undefined = this.tryGetAction(actionName);
134134
if (!action) {
135135
throw new Error(`The action "${actionName}" was not defined`);
136136
}
137137
return action;
138138
}
139139

140+
/**
141+
* Retrieves the action with the specified name. If no matching action is found,
142+
* undefined is returned.
143+
*/
144+
public tryGetAction(actionName: string): CommandLineAction | undefined {
145+
return this._actionsByName.get(actionName);
146+
}
147+
140148
/**
141149
* The program entry point will call this method to begin parsing command-line arguments
142150
* and executing the corresponding action.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ describe('CommandLineParameter', () => {
197197
action.getStringListParameter('--string-list'),
198198
snapshotPropertyNames
199199
);
200+
201+
const copiedArgs: string[] = [];
202+
for (const parameter of action.parameters) {
203+
copiedArgs.push(`### ${parameter.longName} output: ###`);
204+
parameter.appendToArgList(copiedArgs);
205+
}
206+
expect(copiedArgs).toMatchSnapshot();
200207
});
201208
});
202209

@@ -249,6 +256,13 @@ describe('CommandLineParameter', () => {
249256
action.getStringListParameter('--string-list'),
250257
snapshotPropertyNames
251258
);
259+
260+
const copiedArgs: string[] = [];
261+
for (const parameter of action.parameters) {
262+
copiedArgs.push(`### ${parameter.longName} output: ###`);
263+
parameter.appendToArgList(copiedArgs);
264+
}
265+
expect(copiedArgs).toMatchSnapshot();
252266
});
253267
});
254268
});

libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,39 @@ Object {
153153
}
154154
`;
155155

156+
exports[`CommandLineParameter parses an input with ALL parameters 11`] = `
157+
Array [
158+
"### --choice output: ###",
159+
"--choice",
160+
"two",
161+
"### --choice-with-default output: ###",
162+
"--choice-with-default",
163+
"default",
164+
"### --flag output: ###",
165+
"--flag",
166+
"### --integer output: ###",
167+
"--integer",
168+
"123",
169+
"### --integer-with-default output: ###",
170+
"--integer-with-default",
171+
"123",
172+
"### --integer-required output: ###",
173+
"--integer-required",
174+
"321",
175+
"### --string output: ###",
176+
"--string",
177+
"hello",
178+
"### --string-with-default output: ###",
179+
"--string-with-default",
180+
"123",
181+
"### --string-list output: ###",
182+
"--string-list",
183+
"first",
184+
"--string-list",
185+
"second",
186+
]
187+
`;
188+
156189
exports[`CommandLineParameter parses an input with NO parameters 1`] = `
157190
Object {
158191
"argumentName": undefined,
@@ -303,6 +336,28 @@ Object {
303336
}
304337
`;
305338

339+
exports[`CommandLineParameter parses an input with NO parameters 11`] = `
340+
Array [
341+
"### --choice output: ###",
342+
"### --choice-with-default output: ###",
343+
"--choice-with-default",
344+
"default",
345+
"### --flag output: ###",
346+
"### --integer output: ###",
347+
"### --integer-with-default output: ###",
348+
"--integer-with-default",
349+
"123",
350+
"### --integer-required output: ###",
351+
"--integer-required",
352+
"123",
353+
"### --string output: ###",
354+
"### --string-with-default output: ###",
355+
"--string-with-default",
356+
"123",
357+
"### --string-list output: ###",
358+
]
359+
`;
360+
306361
exports[`CommandLineParameter prints the action help 1`] = `
307362
"usage: example do-job [-h] [-c {one,two,three,default}]
308363
[--choice-with-default {one,two,three,default}] [-f]

0 commit comments

Comments
 (0)