Skip to content

Commit b82848c

Browse files
committed
fix(commands): tell option groups apart by the group, not by its schema
Two groups built from one schema object both got values when a command listed one of them.
1 parent baac7b9 commit b82848c

9 files changed

Lines changed: 42 additions & 39 deletions

File tree

‎lib/common/define-command.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ export type OptionsGroupSource = "commandLine" | "startup";
103103

104104
/** What a group reads its values from; the options service implements it. */
105105
export interface OptionValueReader {
106-
/** @param schema The group's schema, which identifies the group. */
107106
readGroupOption(
108-
schema: CommandOptionsSchema,
107+
group: OptionsGroup<any>,
109108
optionName: string,
110109
source: OptionsGroupSource,
111110
): any;
@@ -132,7 +131,10 @@ export class OptionsGroup<
132131
public readonly schema: TSchema,
133132
source: OptionsGroupSource = "commandLine",
134133
) {
135-
super({ factory: () => createGroupValues(schema, source) });
134+
// `this` does not exist until super() returns; the factory runs later.
135+
let group: OptionsGroup<TSchema>;
136+
super({ factory: () => createGroupValues(group, source) });
137+
group = this;
136138
}
137139

138140
public toString(): string {
@@ -143,9 +145,10 @@ export class OptionsGroup<
143145
// Getters rather than values: a service built before the command line is
144146
// parsed may hold the group, and reads it afterwards.
145147
function createGroupValues<TSchema extends CommandOptionsSchema>(
146-
schema: TSchema,
148+
group: OptionsGroup<TSchema>,
147149
source: OptionsGroupSource,
148150
): Readonly<CommandOptionValues<TSchema>> {
151+
const { schema } = group;
149152
const injector = <Injector>getCurrentInjector();
150153
const values: any = {};
151154
for (const optionName of Object.keys(schema)) {
@@ -154,7 +157,7 @@ function createGroupValues<TSchema extends CommandOptionsSchema>(
154157
get: () => {
155158
const value = injector
156159
.get<OptionValueReader>("options")
157-
.readGroupOption(schema, optionName, source);
160+
.readGroupOption(group, optionName, source);
158161
return value === undefined ? schema[optionName].default : value;
159162
},
160163
});

‎lib/common/definitions/commands.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ interface ICommand extends ICommandOptions {
1616
/** @deprecated Declared here, referenced nowhere. */
1717
completionData?: string[];
1818
dashedOptions?: IDictionary<IDashedOption>;
19-
/** The schemas of the option groups the command lists. */
20-
optionGroupSchemas?: readonly object[];
19+
/** The option groups the command lists. */
20+
optionGroups?: readonly object[];
2121
isHierarchicalCommand?: boolean;
2222

2323
/**

‎lib/common/services/command-definition-adapter.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,7 @@ export function createCommandFromDefinition<
535535
return {
536536
allowedParameters: [],
537537
dashedOptions,
538-
optionGroupSchemas: parts
539-
.filter(isOptionsGroup)
540-
.map((group) => group.schema),
538+
optionGroups: parts.filter(isOptionsGroup),
541539
...(definition.disableAnalytics === undefined
542540
? {}
543541
: { disableAnalytics: definition.disableAnalytics }),

‎lib/common/services/commands-service.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export class CommandsService
265265
command && allowsUnknownOptions(command),
266266
this.isExecutingInProcess
267267
? undefined
268-
: (command && command.optionGroupSchemas) || [],
268+
: (command && command.optionGroups) || [],
269269
);
270270
}
271271

‎lib/declarations.d.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,13 +615,13 @@ interface IOptions
615615
ITypingsOptions {
616616
argv: IYargArgv;
617617
/**
618-
* @param commandLineSchemas Given for the command the user typed: the schemas
619-
* of the option groups it lists, which take their values from this parse.
618+
* @param commandLineGroups Given for the command the user typed: the option
619+
* groups it lists, which take their values from this parse.
620620
*/
621621
validateOptions(
622622
commandSpecificDashedOptions?: IDictionary<IDashedOption>,
623623
allowUnknownOptions?: boolean,
624-
commandLineSchemas?: readonly object[],
624+
commandLineGroups?: readonly object[],
625625
): void;
626626
options: IDictionary<IDashedOption>;
627627
shorthands: string[];

‎lib/options.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { inject } from "./common/di";
1515
import { CliOptions } from "./common/contracts/cli-options";
1616
import { COMMAND_LINE_EXPECTED } from "./common/contracts/command-line-expected";
1717
import {
18-
CommandOptionsSchema,
1918
compileOptionsSchema,
19+
OptionsGroup,
2020
OptionsGroupSource,
2121
OptionValueReader,
2222
} from "./common/define-command";
@@ -34,7 +34,7 @@ export class Options implements OptionValueReader {
3434
private initialArgv: yargs.Arguments;
3535
private startupArgv: yargs.Arguments;
3636
private commandLineArgv: yargs.Arguments;
37-
private commandLineSchemas: ReadonlySet<object>;
37+
private commandLineGroups: ReadonlySet<object>;
3838
private commandLineExpected = inject(COMMAND_LINE_EXPECTED);
3939
public argv: yargs.Arguments;
4040
public options: IDictionary<IDashedOption>;
@@ -268,7 +268,7 @@ export class Options implements OptionValueReader {
268268
public validateOptions(
269269
commandSpecificDashedOptions?: IDictionary<IDashedOption>,
270270
allowUnknownOptions?: boolean,
271-
commandLineSchemas?: readonly object[],
271+
commandLineGroups?: readonly object[],
272272
): void {
273273
this.setupOptions(commandSpecificDashedOptions);
274274

@@ -328,9 +328,9 @@ export class Options implements OptionValueReader {
328328
}
329329
}
330330

331-
if (commandLineSchemas && !this.commandLineArgv) {
331+
if (commandLineGroups && !this.commandLineArgv) {
332332
this.commandLineArgv = _.cloneDeep(this.argv);
333-
this.commandLineSchemas = new Set(commandLineSchemas);
333+
this.commandLineGroups = new Set(commandLineGroups);
334334
}
335335
}
336336

@@ -341,7 +341,7 @@ export class Options implements OptionValueReader {
341341
* as a library and has no typed command.
342342
*/
343343
public readGroupOption(
344-
schema: CommandOptionsSchema,
344+
group: OptionsGroup<any>,
345345
optionName: string,
346346
source: OptionsGroupSource,
347347
): any {
@@ -357,7 +357,7 @@ export class Options implements OptionValueReader {
357357
`Inject the option group and read it when it is needed.`,
358358
);
359359
}
360-
return this.commandLineSchemas.has(schema)
360+
return this.commandLineGroups.has(group)
361361
? this.commandLineArgv[optionName]
362362
: undefined;
363363
}

‎test/commands-service.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface ITestSetup {
99
validatedWith: {
1010
called: boolean;
1111
allowUnknown?: boolean;
12-
commandLineSchemas?: readonly object[];
12+
commandLineGroups?: readonly object[];
1313
};
1414
}
1515

@@ -18,7 +18,7 @@ function createTestInjector(command: ICommand): ITestSetup {
1818
const validatedWith: {
1919
called: boolean;
2020
allowUnknown?: boolean;
21-
commandLineSchemas?: readonly object[];
21+
commandLineGroups?: readonly object[];
2222
} = {
2323
called: false,
2424
};
@@ -37,11 +37,11 @@ function createTestInjector(command: ICommand): ITestSetup {
3737
validateOptions: (
3838
dashedOptions: any,
3939
allowUnknown?: boolean,
40-
commandLineSchemas?: readonly object[],
40+
commandLineGroups?: readonly object[],
4141
): void => {
4242
validatedWith.called = true;
4343
validatedWith.allowUnknown = allowUnknown;
44-
validatedWith.commandLineSchemas = commandLineSchemas;
44+
validatedWith.commandLineGroups = commandLineGroups;
4545
},
4646
});
4747
injector.register("staticConfig", {});
@@ -92,9 +92,9 @@ function createDispatchInjector(command: ICommand): {
9292
validateOptions(
9393
dashedOptions: any,
9494
allowUnknown?: boolean,
95-
commandLineSchemas?: readonly object[],
95+
commandLineGroups?: readonly object[],
9696
): void {
97-
record.commandLineParses += commandLineSchemas ? 1 : 0;
97+
record.commandLineParses += commandLineGroups ? 1 : 0;
9898
record.primedWith.push({ dashedOptions, allowUnknown });
9999
// The real parser merges the command's declarations into the shared
100100
// table and re-parses, replacing both.
@@ -193,20 +193,20 @@ describe("commands-service", () => {
193193
await (<any>service).tryExecuteCommandAction("info", []);
194194

195195
assert.isTrue(validatedWith.called);
196-
assert.deepEqual(validatedWith.commandLineSchemas, []);
196+
assert.deepEqual(validatedWith.commandLineGroups, []);
197197
});
198198

199199
it("passes the option groups the typed command lists", async () => {
200-
const schema = {};
200+
const group = {};
201201
const { injector, validatedWith } = createTestInjector({
202202
...baseCommand,
203-
optionGroupSchemas: [schema],
203+
optionGroups: [group],
204204
});
205205
const commandsService: any = injector.resolve(CommandsService);
206206

207207
await commandsService.tryExecuteCommandAction("command", []);
208208

209-
assert.strictEqual(validatedWith.commandLineSchemas[0], schema);
209+
assert.strictEqual(validatedWith.commandLineGroups[0], group);
210210
});
211211

212212
it("tolerates unknown options for a command that forwards them", async () => {

‎test/define-command.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,7 +3547,7 @@ describe("defineCommand", () => {
35473547

35483548
describe("injected values", () => {
35493549
const reader = (values: any) => ({
3550-
readGroupOption: (_schema: any, optionName: string): any =>
3550+
readGroupOption: (_group: any, optionName: string): any =>
35513551
values[optionName],
35523552
});
35533553

@@ -3645,8 +3645,8 @@ describe("defineCommand", () => {
36453645
createTestInjector(),
36463646
);
36473647

3648-
assert.deepEqual(command.optionGroupSchemas, [Listed.schema]);
3649-
assert.strictEqual(command.optionGroupSchemas[0], Listed.schema);
3648+
assert.lengthOf(command.optionGroups, 1);
3649+
assert.strictEqual(command.optionGroups[0], Listed);
36503650
});
36513651

36523652
it("compiles every part into dashedOptions", () => {

‎test/options.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe("options", () => {
100100
/The option 'release' was read before the command line was parsed/,
101101
);
102102
options.validateOptions({ device: { type: OptionType.String } }, false, [
103-
Group.schema,
103+
Group,
104104
]);
105105
assert.isTrue(injected.release);
106106
assert.strictEqual(injected.device, "any");
@@ -111,7 +111,7 @@ describe("options", () => {
111111
options.validateOptions(
112112
{ device: { type: OptionType.String, default: "typed" } },
113113
false,
114-
[Group.schema],
114+
[Group],
115115
);
116116
options.validateOptions({
117117
device: { type: OptionType.String, default: "nested" },
@@ -124,7 +124,7 @@ describe("options", () => {
124124
it("do not see later writes to the options service", () => {
125125
const { injected, options } = setup([], true);
126126
options.validateOptions({ device: { type: OptionType.String } }, false, [
127-
Group.schema,
127+
Group,
128128
]);
129129
options.device = "written";
130130

@@ -144,7 +144,7 @@ describe("options", () => {
144144
options.validateOptions(
145145
{ device: { type: OptionType.String, default: "typed" } },
146146
false,
147-
[Group.schema],
147+
[Group],
148148
);
149149
assert.strictEqual(injected.device, "typed");
150150
});
@@ -157,14 +157,16 @@ describe("options", () => {
157157
release: stringOption({ default: "text" }),
158158
});
159159
const { injected, options } = setup(["--device", "a", "--release"], true);
160+
const sameSchema = testInjector.get(defineOptions(Group.schema));
160161
const other = testInjector.get(Other);
161162
const otherType = testInjector.get(OtherType);
162163
options.validateOptions({ device: { type: OptionType.String } }, false, [
163-
Group.schema,
164+
Group,
164165
]);
165166

166167
assert.strictEqual(injected.device, "a");
167168
assert.strictEqual(other.device, "other");
169+
assert.strictEqual(sameSchema.device, "any");
168170
assert.strictEqual(otherType.release, "text");
169171
});
170172

0 commit comments

Comments
 (0)