( command: string, args: string[], spec: CommandSpec | null, )
| 88 | } |
| 89 | |
| 90 | export async function buildPrefix( |
| 91 | command: string, |
| 92 | args: string[], |
| 93 | spec: CommandSpec | null, |
| 94 | ): Promise<string> { |
| 95 | const maxDepth = await calculateDepth(command, args, spec) |
| 96 | const parts = [command] |
| 97 | const hasSubcommands = !!spec?.subcommands?.length |
| 98 | let foundSubcommand = false |
| 99 | |
| 100 | for (let i = 0; i < args.length; i++) { |
| 101 | const arg = args[i] |
| 102 | if (!arg || parts.length >= maxDepth) break |
| 103 | |
| 104 | if (arg.startsWith('-')) { |
| 105 | // Special case: python -c should stop after -c |
| 106 | if (arg === '-c' && ['python', 'python3'].includes(command.toLowerCase())) |
| 107 | break |
| 108 | |
| 109 | // Check for isCommand/isModule flags that should be included in prefix |
| 110 | if (spec?.options) { |
| 111 | const option = spec.options.find(opt => |
| 112 | Array.isArray(opt.name) ? opt.name.includes(arg) : opt.name === arg, |
| 113 | ) |
| 114 | if ( |
| 115 | option?.args && |
| 116 | toArray(option.args).some(a => a?.isCommand || a?.isModule) |
| 117 | ) { |
| 118 | parts.push(arg) |
| 119 | continue |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // For commands with subcommands, skip global flags to find the subcommand |
| 124 | if (hasSubcommands && !foundSubcommand) { |
| 125 | if (flagTakesArg(arg, args[i + 1], spec)) i++ |
| 126 | continue |
| 127 | } |
| 128 | break // Stop at flags (original behavior) |
| 129 | } |
| 130 | |
| 131 | if (await shouldStopAtArg(arg, args.slice(0, i), spec)) break |
| 132 | if (hasSubcommands && !foundSubcommand) { |
| 133 | foundSubcommand = isKnownSubcommand(arg, spec) |
| 134 | } |
| 135 | parts.push(arg) |
| 136 | } |
| 137 | |
| 138 | return parts.join(' ') |
| 139 | } |
| 140 | |
| 141 | async function calculateDepth( |
| 142 | command: string, |
no test coverage detected