| 1 | | /// The `--option value` pairs of a command whose options each take one value, once. |
| 2 | | struct OptionValues { |
| 3 | 21 | private var values: [String: String] = [:] |
| 4 | | |
| 5 | | /// - Throws: `ReportOptionsError` for an option outside `allowed` or without its value. |
| 6 | 21 | init(arguments: [String], allowed: Set<String>) throws { |
| 7 | 21 | var index = arguments.startIndex |
| 8 | 72 | while index < arguments.endIndex { |
| 9 | 57 | let option = arguments[index] |
| 10 | 57 | guard allowed.contains(option) else { |
| 11 | 3 | throw ReportOptionsError.unknownOption(option) |
| 12 | 54 | } |
| 13 | 54 | guard index + 1 < arguments.endIndex else { |
| 14 | 3 | throw ReportOptionsError.missingValue(option: option) |
| 15 | 51 | } |
| 16 | 51 | values[option] = arguments[index + 1] |
| 17 | 51 | index += 2 |
| 18 | 51 | } |
| 19 | 15 | } |
| 20 | | |
| 21 | 42 | subscript(option: String) -> String? { values[option] } |
| 22 | | |
| 23 | 28 | func required(_ option: String) throws -> String { |
| 24 | 28 | guard let value = values[option] else { throw ReportOptionsError.missingOption(option) } |
| 25 | 23 | return value |
| 26 | 28 | } |
| 27 | | } |