Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,34 @@ function buildSchema(options: OptionDef[]): FlagSchema {

/**
* Quick scan: collect positional (non-dash) args to determine the command path.
* Does not consume flag values — just skips dash-prefixed tokens.
* Skips global flags and their values so that e.g. `--output json text chat`
* correctly produces ['text', 'chat'] instead of ['json', 'text', 'chat'].
*/
export function scanCommandPath(argv: string[]): string[] {
export function scanCommandPath(argv: string[], globalOptions: OptionDef[] = []): string[] {
const globalSchema = buildSchema(globalOptions);
const path: string[] = [];
for (const arg of argv) {
let i = 0;
while (i < argv.length) {
const arg = argv[i]!;
if (arg === '--') break;
if (!arg.startsWith('-')) path.push(arg);

if (arg.startsWith('--')) {
const eqIdx = arg.indexOf('=');
const key = eqIdx !== -1 ? arg.slice(2, eqIdx) : arg.slice(2);
const camelKey = kebabToCamel(key);

if (!globalSchema.booleans.has(camelKey) && eqIdx === -1) {
i += 2;
} else {
i += 1;
}
continue;
}

if (arg.startsWith('-')) { i++; continue; }

path.push(arg);
i++;
}
return path;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function main() {
process.exit(0);
}

const commandPath = scanCommandPath(argv);
const commandPath = scanCommandPath(argv, GLOBAL_OPTIONS);

if (argv.includes('--help') || argv.includes('-h')) {
registry.printHelp(commandPath, process.stderr);
Expand Down
Loading