( flag: string, nextArg: string | undefined, spec: CommandSpec | null, )
| 49 | |
| 50 | // Check if a flag takes an argument based on spec, or use heuristic |
| 51 | function flagTakesArg( |
| 52 | flag: string, |
| 53 | nextArg: string | undefined, |
| 54 | spec: CommandSpec | null, |
| 55 | ): boolean { |
| 56 | // Check if flag is in spec.options |
| 57 | if (spec?.options) { |
| 58 | const option = spec.options.find(opt => |
| 59 | Array.isArray(opt.name) ? opt.name.includes(flag) : opt.name === flag, |
| 60 | ) |
| 61 | if (option) return !!option.args |
| 62 | } |
| 63 | // Heuristic: if next arg isn't a flag and isn't a known subcommand, assume it's a flag value |
| 64 | if (spec?.subcommands?.length && nextArg && !nextArg.startsWith('-')) { |
| 65 | return !isKnownSubcommand(nextArg, spec) |
| 66 | } |
| 67 | return false |
| 68 | } |
| 69 | |
| 70 | // Find the first subcommand by skipping flags and their values |
| 71 | function findFirstSubcommand( |
no test coverage detected