Skip to content

Commit e22d2b9

Browse files
Elaine YINclaude
authored andcommitted
fix(args): support global flags before command name
scanCommandPath now accepts globalOptions schema to correctly skip flag values (e.g. --output json, --region cn) before the command path. Previously `minimax --output json text chat` would fail with "Unknown command: minimax json text chat". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 920273d commit e22d2b9

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/args.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,34 @@ function buildSchema(options: OptionDef[]): FlagSchema {
4040

4141
/**
4242
* Quick scan: collect positional (non-dash) args to determine the command path.
43-
* Does not consume flag values — just skips dash-prefixed tokens.
43+
* Skips global flags and their values so that e.g. `--output json text chat`
44+
* correctly produces ['text', 'chat'] instead of ['json', 'text', 'chat'].
4445
*/
45-
export function scanCommandPath(argv: string[]): string[] {
46+
export function scanCommandPath(argv: string[], globalOptions: OptionDef[] = []): string[] {
47+
const globalSchema = buildSchema(globalOptions);
4648
const path: string[] = [];
47-
for (const arg of argv) {
49+
let i = 0;
50+
while (i < argv.length) {
51+
const arg = argv[i]!;
4852
if (arg === '--') break;
49-
if (!arg.startsWith('-')) path.push(arg);
53+
54+
if (arg.startsWith('--')) {
55+
const eqIdx = arg.indexOf('=');
56+
const key = eqIdx !== -1 ? arg.slice(2, eqIdx) : arg.slice(2);
57+
const camelKey = kebabToCamel(key);
58+
59+
if (!globalSchema.booleans.has(camelKey) && eqIdx === -1) {
60+
i += 2;
61+
} else {
62+
i += 1;
63+
}
64+
continue;
65+
}
66+
67+
if (arg.startsWith('-')) { i++; continue; }
68+
69+
path.push(arg);
70+
i++;
5071
}
5172
return path;
5273
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function main() {
3535
process.exit(0);
3636
}
3737

38-
const commandPath = scanCommandPath(argv);
38+
const commandPath = scanCommandPath(argv, GLOBAL_OPTIONS);
3939

4040
if (argv.includes('--help') || argv.includes('-h')) {
4141
registry.printHelp(commandPath, process.stderr);

0 commit comments

Comments
 (0)