Parses the specified command line. The command line. The options.
(string commandLine)
| 235 | /// <param name="commandLine">The command line.</param> |
| 236 | /// <returns>The options.</returns> |
| 237 | public static Option[] Parse(string commandLine) |
| 238 | { |
| 239 | var options = new List<Option>(); |
| 240 | var length = commandLine.Length; |
| 241 | |
| 242 | for (int i = 0; i < length;) |
| 243 | { |
| 244 | // Skip white space |
| 245 | while (i < length && char.IsWhiteSpace(commandLine[i])) |
| 246 | i++; |
| 247 | |
| 248 | // Read option prefix |
| 249 | if (i == length) |
| 250 | break; |
| 251 | var wholeQuote = commandLine[i] == '\"'; |
| 252 | if (wholeQuote) |
| 253 | i++; |
| 254 | if (i == length) |
| 255 | break; |
| 256 | if (commandLine[i] == '-') |
| 257 | i++; |
| 258 | else if (commandLine[i] == '/') |
| 259 | i++; |
| 260 | |
| 261 | // Skip white space |
| 262 | while (i < length && char.IsWhiteSpace(commandLine[i])) |
| 263 | i++; |
| 264 | |
| 265 | // Read option name |
| 266 | int nameStart = i; |
| 267 | while (i < length && commandLine[i] != '-' && commandLine[i] != '=' && !char.IsWhiteSpace(commandLine[i])) |
| 268 | i++; |
| 269 | int nameEnd = i; |
| 270 | string name = commandLine.Substring(nameStart, nameEnd - nameStart); |
| 271 | |
| 272 | // Skip white space |
| 273 | while (i < length && char.IsWhiteSpace(commandLine[i])) |
| 274 | i++; |
| 275 | |
| 276 | // Check if has no value |
| 277 | if (i >= length - 1 || commandLine[i] != '=') |
| 278 | { |
| 279 | options.Add(new Option |
| 280 | { |
| 281 | Name = name, |
| 282 | Value = string.Empty |
| 283 | }); |
| 284 | if (wholeQuote) |
| 285 | i++; |
| 286 | if (i < length && commandLine[i] != '\"') |
| 287 | i++; |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | // Read value |
| 292 | i++; |
| 293 | int valueStart, valueEnd; |
| 294 | if (commandLine.Length > i + 1 && commandLine[i] == '\\' && commandLine[i + 1] == '\"') |