Parse the command line.
(args: string[])
| 122 | |
| 123 | /** Parse the command line. */ |
| 124 | function parseOptions(args: string[]): Options { |
| 125 | const { values, tokens } = parseArgs({ |
| 126 | args, |
| 127 | strict: false, |
| 128 | tokens: true, |
| 129 | allowPositionals: true, |
| 130 | allowNegative: true, |
| 131 | options: CLI_OPTION_DEFINITIONS, |
| 132 | }); |
| 133 | |
| 134 | const builderOptions: json.JsonObject = {}; |
| 135 | const positionals: string[] = []; |
| 136 | |
| 137 | for (let i = 0; i < tokens.length; i++) { |
| 138 | const token = tokens[i]; |
| 139 | |
| 140 | if (token.kind === 'positional') { |
| 141 | positionals.push(token.value); |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (token.kind !== 'option') { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | const name = token.name; |
| 150 | let value: JsonValue = token.value ?? true; |
| 151 | |
| 152 | // `parseArgs` already handled known boolean args and their --no- forms. |
| 153 | // Only process options not in CLI_OPTION_DEFINITIONS here. |
| 154 | if (name in CLI_OPTION_DEFINITIONS) { |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | if (/[A-Z]/.test(name)) { |
| 159 | throw new Error( |
| 160 | `Unknown argument ${name}. Did you mean ${strings.decamelize(name).replaceAll('_', '-')}?`, |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | // Handle --no-flag for unknown options, treating it as false |
| 165 | if (name.startsWith('no-')) { |
| 166 | const realName = name.slice(3); |
| 167 | builderOptions[strings.camelize(realName)] = false; |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | // Handle value for unknown options |
| 172 | if (token.inlineValue === undefined) { |
| 173 | // Look ahead |
| 174 | const nextToken = tokens[i + 1]; |
| 175 | if (nextToken?.kind === 'positional') { |
| 176 | value = nextToken.value; |
| 177 | i++; // Consume next token |
| 178 | } else { |
| 179 | value = true; // Treat as boolean if no value follows |
| 180 | } |
| 181 | } |