Skip to content

Commit 56798ae

Browse files
authored
fix(cli): accept inherited flags after nested subcommands (#116587)
* fix(cli): accept auth agent flag after subcommands * openclaw-87a: normalize inherited skills options * test(cli): classify curator leaf JSON flags * test(cli): cover all auth agent leaves
1 parent 2d9a3f5 commit 56798ae

10 files changed

Lines changed: 323 additions & 94 deletions

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Skills own workflows; root owns hard policy and routing. Product direction and m
102102
- OpenAI Codex = `openai`. No new/live `openai-codex` routes — legacy input only; runtime/setup/auth/catalog use `openai` + `openai/*`, doctor/migrations repair stale `openai-codex/*` profiles/metadata.
103103
- Config/env surface bar is high; `openclaw.json` and env vars are already large. Before adding an option or env var, prove existing product behavior, provider selection, defaults, or doctor migration cannot solve it; prefer removing/consolidating options when touching these surfaces.
104104
- CLI setup flows (`openclaw onboard`/`configure`, documented flags, non-interactive behavior, generated config shape) are shipped public API once external docs/installers can copy them: prefer additive flags/aliases, deprecation windows, and backward-preserving migrations over breaking existing snippets.
105+
- Nested CLI options: when a parent option semantically applies to a leaf subcommand, declare it on both the parent and every applicable leaf so positional parsing accepts the option before or after the subcommand. Resolve the leaf value only when its source is non-default, then inherit from ancestors with `inheritOptionFromParent`. Do not expose inherited options on leaves where the semantics differ. Add real-parser coverage that enumerates every applicable leaf.
105106
- New binary fallible-operation results use `Result` from `@openclaw/normalization-core/result`; domain-rich outcomes keep named discriminated unions.
106107
- Tests may use observed examples, but prod literals need a short contract reason.
107108
- Compatibility is opt-in. "Shipped" means reachable from a release Git tag; main/GitHub/PR/unreleased code is not shipped.

‎docs/cli/models.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Manages `agents.defaults.model.fallbacks`. `openclaw models image-fallbacks list
152152
```bash
153153
openclaw models auth add
154154
openclaw models auth list [--provider <id>] [--json]
155-
openclaw models auth login --provider <id>
155+
openclaw models auth login --provider <id> [--agent <agentId>]
156156
openclaw models auth login --provider openai --profile-id openai:work
157157
openclaw models auth login-github-copilot
158158
openclaw models auth logout <profileId> [--yes]
@@ -174,7 +174,7 @@ openclaw models auth order clear --provider <id>
174174

175175
`models auth login-github-copilot` is a shortcut for `models auth login --provider github-copilot --method device` (GitHub device flow); it accepts `--yes` to overwrite an existing profile without prompting.
176176

177-
Use `openclaw models auth --agent <id> <subcommand>` to write auth results to a specific configured agent store. The parent `--agent` flag is honored by `add`, `list`, `login`, `logout`, `paste-api-key`, `setup-token`, `paste-token`, `login-github-copilot`, and `order get`/`set`/`clear`.
177+
Use either `openclaw models auth --agent <id> <subcommand>` or `openclaw models auth <subcommand> --agent <id>` to target a specific configured agent store. Both forms are supported by `add`, `list`, `login`, `logout`, `paste-api-key`, `setup-token`, `paste-token`, `login-github-copilot`, and `order get`/`set`/`clear`.
178178

179179
For OpenAI models, `--provider openai` defaults to ChatGPT/Codex account login. Use `--method api-key` only when you want to add an OpenAI API-key profile, usually as a backup for Codex subscription limits. Run `openclaw doctor --fix` to migrate older legacy OpenAI Codex prefix auth/profile state to `openai`.
180180

‎docs/cli/skills.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Notes:
136136
| `verify --card` | Prints the generated Skill Card Markdown instead of JSON. Exits non-zero when ClawHub returns `ok: false` or `decision: "fail"`; unsigned signatures are informational unless ClawHub policy changes. |
137137
| Skill Card fingerprint | Installed ClawHub bundles can include a generated `skill-card.md`. OpenClaw treats verification as a ClawHub server decision and does not reject an installed skill just because that generated card changes the bundle fingerprint. |
138138
| `check --agent <id>` | Checks the selected agent's workspace and reports which ready skills are actually visible to that agent's prompt or command surface. |
139+
| `workshop --agent <id>` | Accepted before or after a Workshop leaf command, for example `workshop --agent <id> list` or `workshop list --agent <id>`. If both are provided, the leaf value wins. |
140+
| `curator --json` | Accepted before or after a Curator leaf command, for example `curator --json status` or `curator status --json`. |
139141
| `list` | Default action when no subcommand is provided. |
140142
| `list`/`info`/`check` output | Rendered output goes to stdout. With `--json`, the machine-readable payload stays on stdout for pipes and scripts. |
141143
| `curator status --json` | Returns legacy age-based lifecycle state written by older releases. Daily collection review does not use this state. |

‎src/cli/models-cli.test.ts‎

Lines changed: 135 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const mocks = vi.hoisted(() => ({
1515
modelsAuthListCommand: vi.fn().mockResolvedValue(undefined),
1616
modelsAuthLoginCommand: vi.fn().mockResolvedValue(undefined),
1717
modelsAuthLogoutCommand: vi.fn().mockResolvedValue(undefined),
18+
modelsAuthOrderClearCommand: vi.fn().mockResolvedValue(undefined),
19+
modelsAuthOrderGetCommand: vi.fn().mockResolvedValue(undefined),
20+
modelsAuthOrderSetCommand: vi.fn().mockResolvedValue(undefined),
1821
modelsAuthPasteApiKeyCommand: vi.fn().mockResolvedValue(undefined),
1922
modelsAuthPasteTokenCommand: vi.fn().mockResolvedValue(undefined),
2023
modelsAuthSetupTokenCommand: vi.fn().mockResolvedValue(undefined),
@@ -25,6 +28,9 @@ const {
2528
modelsAuthListCommand,
2629
modelsAuthLoginCommand,
2730
modelsAuthLogoutCommand,
31+
modelsAuthOrderClearCommand,
32+
modelsAuthOrderGetCommand,
33+
modelsAuthOrderSetCommand,
2834
modelsAuthPasteApiKeyCommand,
2935
modelsAuthPasteTokenCommand,
3036
modelsAuthSetupTokenCommand,
@@ -53,9 +59,9 @@ vi.mock("../commands/models/auth-logout.js", () => ({
5359
modelsAuthLogoutCommand: mocks.modelsAuthLogoutCommand,
5460
}));
5561
vi.mock("../commands/models/auth-order.js", () => ({
56-
modelsAuthOrderClearCommand: mocks.noopAsync,
57-
modelsAuthOrderGetCommand: mocks.noopAsync,
58-
modelsAuthOrderSetCommand: mocks.noopAsync,
62+
modelsAuthOrderClearCommand: mocks.modelsAuthOrderClearCommand,
63+
modelsAuthOrderGetCommand: mocks.modelsAuthOrderGetCommand,
64+
modelsAuthOrderSetCommand: mocks.modelsAuthOrderSetCommand,
5965
}));
6066
vi.mock("../commands/models/aliases.js", () => ({
6167
modelsAliasesAddCommand: mocks.noopAsync,
@@ -91,6 +97,9 @@ describe("models cli", () => {
9197
modelsAuthListCommand.mockClear();
9298
modelsAuthLoginCommand.mockClear();
9399
modelsAuthLogoutCommand.mockClear();
100+
modelsAuthOrderClearCommand.mockClear();
101+
modelsAuthOrderGetCommand.mockClear();
102+
modelsAuthOrderSetCommand.mockClear();
94103
modelsAuthPasteApiKeyCommand.mockClear();
95104
modelsAuthPasteTokenCommand.mockClear();
96105
modelsAuthSetupTokenCommand.mockClear();
@@ -100,14 +109,17 @@ describe("models cli", () => {
100109
});
101110

102111
function createProgram() {
103-
const program = new Command();
112+
const program = new Command().enablePositionalOptions();
104113
registerModelsCli(program);
105114
return program;
106115
}
107116

108117
async function runModelsCommand(args: string[]) {
109118
await runRegisteredCli({
110-
register: registerModelsCli as (program: Command) => void,
119+
register: (program: Command) => {
120+
program.enablePositionalOptions();
121+
registerModelsCli(program);
122+
},
111123
argv: args,
112124
});
113125
}
@@ -197,6 +209,17 @@ describe("models cli", () => {
197209
});
198210
});
199211

212+
it("declares --agent on every agent-aware auth leaf command", () => {
213+
const models = requireCommand(createProgram(), "models");
214+
const auth = requireCommand(models, "auth");
215+
const order = requireCommand(auth, "order");
216+
const authLeaves = auth.commands.filter((command) => command !== order);
217+
218+
for (const command of [...authLeaves, ...order.commands]) {
219+
expect(command.options.some((option) => option.long === "--agent")).toBe(true);
220+
}
221+
});
222+
200223
it.each([
201224
{ label: "status flag", args: ["models", "status", "--agent", "poe"] },
202225
{ label: "parent flag", args: ["models", "--agent", "poe", "status"] },
@@ -268,6 +291,113 @@ describe("models cli", () => {
268291
expectCommandOptions(command, expected);
269292
});
270293

294+
it.each([
295+
{
296+
label: "add",
297+
args: ["models", "auth", "add", "--agent", "poe"],
298+
command: modelsAuthAddCommand,
299+
expected: { agent: "poe" },
300+
},
301+
{
302+
label: "list",
303+
args: ["models", "auth", "list", "--provider", "openai", "--agent", "poe"],
304+
command: modelsAuthListCommand,
305+
expected: { agent: "poe", provider: "openai" },
306+
},
307+
{
308+
label: "login",
309+
args: ["models", "auth", "login", "--provider", "openai", "--agent", "poe"],
310+
command: modelsAuthLoginCommand,
311+
expected: { agent: "poe", provider: "openai" },
312+
},
313+
{
314+
label: "logout",
315+
args: ["models", "auth", "logout", "openai:manual", "--yes", "--agent", "poe"],
316+
command: modelsAuthLogoutCommand,
317+
expected: { agent: "poe", profileId: "openai:manual", yes: true },
318+
},
319+
{
320+
label: "setup-token",
321+
args: ["models", "auth", "setup-token", "--provider", "anthropic", "--agent", "poe"],
322+
command: modelsAuthSetupTokenCommand,
323+
expected: { agent: "poe", provider: "anthropic" },
324+
},
325+
{
326+
label: "paste-token",
327+
args: ["models", "auth", "paste-token", "--provider", "anthropic", "--agent", "poe"],
328+
command: modelsAuthPasteTokenCommand,
329+
expected: { agent: "poe", provider: "anthropic" },
330+
},
331+
{
332+
label: "paste-api-key",
333+
args: ["models", "auth", "paste-api-key", "--provider", "openai", "--agent", "poe"],
334+
command: modelsAuthPasteApiKeyCommand,
335+
expected: { agent: "poe", provider: "openai" },
336+
},
337+
{
338+
label: "login-github-copilot",
339+
args: ["models", "auth", "login-github-copilot", "--agent", "poe", "--yes"],
340+
command: modelsAuthLoginCommand,
341+
expected: { agent: "poe", provider: "github-copilot", method: "device", yes: true },
342+
},
343+
{
344+
label: "order get",
345+
args: ["models", "auth", "order", "get", "--provider", "anthropic", "--agent", "poe"],
346+
command: modelsAuthOrderGetCommand,
347+
expected: { agent: "poe", provider: "anthropic" },
348+
},
349+
{
350+
label: "order set",
351+
args: [
352+
"models",
353+
"auth",
354+
"order",
355+
"set",
356+
"--provider",
357+
"anthropic",
358+
"anthropic:first",
359+
"anthropic:second",
360+
"--agent",
361+
"poe",
362+
],
363+
command: modelsAuthOrderSetCommand,
364+
expected: {
365+
agent: "poe",
366+
provider: "anthropic",
367+
order: ["anthropic:first", "anthropic:second"],
368+
},
369+
},
370+
{
371+
label: "order clear",
372+
args: ["models", "auth", "order", "clear", "--provider", "anthropic", "--agent", "poe"],
373+
command: modelsAuthOrderClearCommand,
374+
expected: { agent: "poe", provider: "anthropic" },
375+
},
376+
])("passes leaf --agent to models auth $label", async ({ args, command, expected }) => {
377+
await runModelsCommand(args);
378+
379+
expectCommandOptions(command, expected);
380+
});
381+
382+
it("prefers leaf --agent when both models auth forms are present", async () => {
383+
await runModelsCommand([
384+
"models",
385+
"auth",
386+
"--agent",
387+
"parent",
388+
"login",
389+
"--agent",
390+
"leaf",
391+
"--provider",
392+
"openai",
393+
]);
394+
395+
expectCommandOptions(modelsAuthLoginCommand, {
396+
agent: "leaf",
397+
provider: "openai",
398+
});
399+
});
400+
271401
it("passes --method through models auth login", async () => {
272402
await runModelsCommand([
273403
"models",

‎src/cli/models-cli.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,10 @@ export function registerModelsCli(program: Command) {
337337
auth
338338
.command("add")
339339
.description("Interactive auth helper (provider auth or paste token)")
340-
.action(async (command) => {
340+
.option("--agent <id>", "Agent id (default: configured default agent)")
341+
.action(async (opts, command) => {
341342
await withModelsRuntime(async ({ defaultRuntime, resolveModelAgentOption }) => {
342-
const agent = resolveModelAgentOption(command) ?? resolveModelAgentOption(auth);
343+
const agent = resolveModelAgentOption(command, opts);
343344
const { modelsAuthAddCommand } = await loadModelsAuthCommands();
344345
await modelsAuthAddCommand({ agent }, defaultRuntime);
345346
});
@@ -369,6 +370,7 @@ export function registerModelsCli(program: Command) {
369370
auth
370371
.command("login")
371372
.description("Run a provider plugin auth flow (OAuth/API key)")
373+
.option("--agent <id>", "Agent id (default: configured default agent)")
372374
.option("--provider <id>", "Provider id registered by a plugin")
373375
.option("--method <id>", "Provider auth method id")
374376
.option("--device-code", "Use the provider device-code auth method", false)
@@ -405,6 +407,7 @@ export function registerModelsCli(program: Command) {
405407
auth
406408
.command("setup-token")
407409
.description("Run a provider CLI to create/sync a token (TTY required)")
410+
.option("--agent <id>", "Agent id (default: configured default agent)")
408411
.option("--provider <name>", "Provider id")
409412
.option("--yes", "Skip confirmation", false)
410413
.action(async (opts, command) => {
@@ -425,6 +428,7 @@ export function registerModelsCli(program: Command) {
425428
auth
426429
.command("paste-token")
427430
.description("Paste a token into auth-profiles.json and update config")
431+
.option("--agent <id>", "Agent id (default: configured default agent)")
428432
.requiredOption("--provider <name>", "Provider id (e.g. anthropic)")
429433
.option("--profile-id <id>", "Auth profile id (default: <provider>:manual)")
430434
.option(
@@ -450,6 +454,7 @@ export function registerModelsCli(program: Command) {
450454
auth
451455
.command("paste-api-key")
452456
.description("Paste an API key into auth-profiles.json and update config")
457+
.option("--agent <id>", "Agent id (default: configured default agent)")
453458
.requiredOption("--provider <name>", "Provider id (e.g. openai)")
454459
.option("--profile-id <id>", "Auth profile id (default: <provider>:manual)")
455460
.action(async (opts, command) => {
@@ -470,6 +475,7 @@ export function registerModelsCli(program: Command) {
470475
auth
471476
.command("login-github-copilot")
472477
.description("Login to GitHub Copilot via GitHub device flow (TTY required)")
478+
.option("--agent <id>", "Agent id (default: configured default agent)")
473479
.option("--yes", "Overwrite existing profile without prompting", false)
474480
.action(async (opts, command) => {
475481
await withModelsRuntime(async ({ defaultRuntime, resolveModelAgentOption }) => {

‎src/cli/program/root-command-descriptions.test.ts‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,6 @@ const JSON_NOT_APPLICABLE = {
218218
},
219219
} as const;
220220

221-
// These subcommands intentionally consume --json from their parent and emit JSON.
222-
const JSON_OUTPUT_INHERITED_FROM_PARENT = new Set([
223-
"skills curator status",
224-
"skills curator pin",
225-
"skills curator unpin",
226-
"skills curator restore",
227-
]);
228-
229221
// Route-first parsing accepts JSON before Commander registration is reached.
230222
const JSON_OUTPUT_ROUTE_FIRST = new Set(["agents"]);
231223

@@ -247,26 +239,13 @@ function hasOwnJsonOption(command: Command): boolean {
247239
return command.options.some((option) => option.long === "--json");
248240
}
249241

250-
function hasAncestorJsonOption(command: Command): boolean {
251-
for (let parent = command.parent; parent; parent = parent.parent) {
252-
if (hasOwnJsonOption(parent)) {
253-
return true;
254-
}
255-
}
256-
return false;
257-
}
258-
259242
function supportsJsonOutput(path: string, command: Command): boolean {
260243
// `config set --json` is a legacy strict-input parser alias. Only its
261244
// `--dry-run --json` combination reports JSON, so the mutation stays N/A.
262245
if (path === "config set") {
263246
return false;
264247
}
265-
return (
266-
hasOwnJsonOption(command) ||
267-
(JSON_OUTPUT_INHERITED_FROM_PARENT.has(path) && hasAncestorJsonOption(command)) ||
268-
JSON_OUTPUT_ROUTE_FIRST.has(path)
269-
);
248+
return hasOwnJsonOption(command) || JSON_OUTPUT_ROUTE_FIRST.has(path);
270249
}
271250

272251
function collectRegisteredCommandPaths(...programs: Command[]): Set<string> {
@@ -415,15 +394,6 @@ describe("root command descriptions", () => {
415394
"remove stale JSON N/A entries after adding output support",
416395
).toEqual([]);
417396

418-
const staleInheritedSupport = [...JSON_OUTPUT_INHERITED_FROM_PARENT].filter((path) => {
419-
const command = registered.get(path);
420-
return !command || hasOwnJsonOption(command) || !hasAncestorJsonOption(command);
421-
});
422-
expect(
423-
staleInheritedSupport,
424-
"inherited JSON entries must exist, lack their own flag, and inherit a parent flag",
425-
).toEqual([]);
426-
427397
const staleRouteFirstSupport = [...JSON_OUTPUT_ROUTE_FIRST].filter((path) => {
428398
const command = registered.get(path);
429399
return !command || hasOwnJsonOption(command);

‎src/cli/skills-cli.commands.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ function expectLogger(value: unknown): void {
211211
}
212212
}
213213

214+
function requireCommand(parent: Command, name: string): Command {
215+
const command = parent.commands.find((candidate) => candidate.name() === name);
216+
if (!command) {
217+
throw new Error(`missing command: ${name}`);
218+
}
219+
return command;
220+
}
221+
214222
function expectStatusWorkspaceCall(workspaceDir: string): void {
215223
const [actualWorkspaceDir, options] = mockCall(buildWorkspaceSkillStatusMock);
216224
expect(actualWorkspaceDir).toBe(workspaceDir);
@@ -758,6 +766,27 @@ describe("skills cli commands", () => {
758766
});
759767
});
760768

769+
it("declares inherited options on every applicable nested leaf", () => {
770+
const program = new Command().enablePositionalOptions();
771+
registerSkillsCli(program);
772+
const skills = requireCommand(program, "skills");
773+
const workshop = requireCommand(skills, "workshop");
774+
const curator = requireCommand(skills, "curator");
775+
776+
for (const command of workshop.commands) {
777+
expect(
778+
command.options.some((option) => option.long === "--agent"),
779+
command.name(),
780+
).toBe(true);
781+
}
782+
for (const command of curator.commands) {
783+
expect(
784+
command.options.some((option) => option.long === "--json"),
785+
command.name(),
786+
).toBe(true);
787+
}
788+
});
789+
761790
it("rejects --version for git and local source installs", async () => {
762791
await expect(
763792
runCommand(["skills", "install", "git:owner/tools", "--version", "1.2.3"]),

0 commit comments

Comments
 (0)