Skip to content

Commit 6fe691f

Browse files
committed
fix(commands): reject a command that lists two groups sharing a spelling
With equal specs it was accepted, and both groups read the same value.
1 parent b82848c commit 6fe691f

4 files changed

Lines changed: 46 additions & 16 deletions

File tree

‎.agents/skills/cli-options/SKILL.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ Hooks change an operation through its payload.
5151
not count. A group the typed command does not list reads as its defaults, which is what
5252
keeps two options with one spelling apart.
5353
- One group per meaning. `--sdk` is a simulator version for one command and an Android
54-
target for another, so it is two groups.
54+
target for another, so it is two groups. A command cannot list two groups that share a
55+
spelling; put an option several topics need into a group of its own.
5556
- Keep the object and read it inside methods. Destructuring it in a field or
5657
constructor reads too early and throws.
5758
- A group is read-only, holds the typed command's values, and nothing changes them later:

‎defining-commands.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ identical spec: the same type, `default`, `alias` and `hasSensitiveValue`.
266266
`description` is not compared. An alias may not equal another option's name
267267
or alias.
268268

269+
Two groups listed by one command may not share a spelling at all, whatever their
270+
specs: both would read the same value. Put an option several groups need into a
271+
group of its own.
272+
269273
`defineCommand` and `Command()` check this when they are called, and
270274
`defineOptions` checks it within its own schema. The message names both
271275
declarations:

‎lib/common/define-command.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,19 @@ const sameOptionSpec = (a: CommandOptionSpec, b: CommandOptionSpec): boolean =>
227227
* Merges what `options` declares into one schema. A spelling — a name or an
228228
* alias — may be declared once, or again with the same spec; two specs for one
229229
* spelling would leave the parser with one meaning and the other declaration
230-
* silently wrong, so that is reported instead.
230+
* silently wrong, so that is reported instead. So are two groups that share a
231+
* spelling, even with the same spec: they would both read one value.
231232
*/
232233
export function resolveCommandOptions(
233234
options: CommandOptionsInput | undefined,
234235
report: (problem: string) => never,
235236
inlineSource: string = "the command's own options",
236237
): ResolvedCommandOptions {
237-
const parts: { source: string; schema: CommandOptionsSchema }[] = [];
238+
const parts: {
239+
source: string;
240+
schema: CommandOptionsSchema;
241+
isGroup?: boolean;
242+
}[] = [];
238243
const groups = new Set<OptionsGroup<any>>();
239244
const list: ReadonlyArray<OptionsGroup<any> | CommandOptionsSchema> =
240245
options === undefined
@@ -250,6 +255,7 @@ export function resolveCommandOptions(
250255
parts.push({
251256
source: String(part),
252257
schema: part.schema,
258+
isGroup: true,
253259
});
254260
}
255261
} else {
@@ -261,10 +267,19 @@ export function resolveCommandOptions(
261267
const schema: CommandOptionsSchema = Object.create(null);
262268
const owners: IDictionary<{ optionName: string; source: string }> =
263269
Object.create(null);
264-
for (const { source, schema: partSchema } of parts) {
270+
const groupOwners = new Map<string, string>();
271+
for (const { source, schema: partSchema, isGroup } of parts) {
265272
for (const optionName of Object.keys(partSchema)) {
266273
const spec = partSchema[optionName];
267274
const existing = schema[optionName];
275+
if (isGroup && groupOwners.has(optionName)) {
276+
report(
277+
`option '--${optionName}' is declared by two option groups, ${groupOwners.get(optionName)} and ${source}; a command gives one spelling to one group`,
278+
);
279+
}
280+
if (isGroup) {
281+
groupOwners.set(optionName, source);
282+
}
268283
if (existing && !sameOptionSpec(existing, spec)) {
269284
report(
270285
`option '--${optionName}' is declared by ${owners[optionName].source} and by ${source} with different specs`,

‎test/define-command.ts‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,9 +3673,6 @@ describe("defineCommand", () => {
36733673
const Shared = defineOptions({
36743674
watch: booleanOption({ default: true }),
36753675
});
3676-
const Other = defineOptions({
3677-
watch: booleanOption(),
3678-
});
36793676

36803677
assert.throws(
36813678
() =>
@@ -3686,15 +3683,28 @@ describe("defineCommand", () => {
36863683
}),
36873684
/^Invalid command definition for 'dctest-grp-clash-inline': option '--watch' is declared by option group \(--watch\) and by the command's own options with different specs/,
36883685
);
3689-
assert.throws(
3690-
() =>
3691-
defineCommand({
3692-
name: "dctest-grp-clash-groups",
3693-
options: [Shared, Other],
3694-
run: (): void => undefined,
3695-
}),
3696-
/option '--watch' is declared by option group \(--watch\) and by option group \(--watch\) with different specs/,
3697-
);
3686+
});
3687+
3688+
it("rejects two groups that share a spelling, whatever their specs", () => {
3689+
const Simulator = defineOptions({ sdk: stringOption() });
3690+
const Target = defineOptions({ sdk: stringOption() });
3691+
const OtherSpec = defineOptions({ sdk: stringOption({ alias: "S" }) });
3692+
const listing = (options: any) => () =>
3693+
defineCommand({
3694+
name: "dctest-grp-two",
3695+
options,
3696+
run: (): void => undefined,
3697+
});
3698+
const inline = { sdk: stringOption() };
3699+
const message =
3700+
/option '--sdk' is declared by two option groups, option group \(--sdk\) and option group \(--sdk\)/;
3701+
3702+
assert.throws(listing([Simulator, Target]), message);
3703+
assert.throws(listing([Simulator, OtherSpec]), message);
3704+
assert.throws(listing([Simulator, inline, Target]), message);
3705+
assert.throws(listing([inline, Simulator, Target]), message);
3706+
assert.doesNotThrow(listing([Simulator, Simulator]));
3707+
assert.doesNotThrow(listing([Simulator, inline]));
36983708
});
36993709

37003710
it("treats a different alias, default or sensitivity as a different spec", () => {

0 commit comments

Comments
 (0)