| 1 | | /// What `--help` and `-h` print, for `covane` and each of its commands. |
| 2 | | public enum CommandHelp { |
| 3 | | /// The help the arguments ask for, or nil when they ask for work. `--help` and `-h` |
| 4 | | /// count only where an option is expected, so either can still be an option's value |
| 5 | | /// (`--subject -h`). |
| 6 | 14 | public static func text(for arguments: [String]) -> String? { |
| 7 | 37 | let command = commands.first { $0.word == arguments.first } |
| 8 | 14 | guard asksForHelp(options: arguments.dropFirst(command == nil ? 0 : 1)) else { |
| 9 | 5 | return nil |
| 10 | 9 | } |
| 11 | 9 | guard let command else { return reportHelp } |
| 12 | 3 | return "\(command.summary).\n\n\(command.usage)" |
| 13 | 14 | } |
| 14 | | |
| 15 | | private struct Command { |
| 16 | | let word: String |
| 17 | | let summary: String |
| 18 | | let usage: String |
| 19 | | } |
| 20 | | |
| 21 | | private static let commands = [ |
| 22 | | Command( |
| 23 | | word: HistoryOptions.commandWord, summary: "Write the page that lists published runs", |
| 24 | | usage: HistoryOptions.usage), |
| 25 | | Command( |
| 26 | | word: PublishOptions.commandWord, summary: "Put a report on a Pages branch", |
| 27 | | usage: PublishOptions.usage), |
| 28 | | Command( |
| 29 | | word: UnavailableOptions.commandWord, summary: "Write a run that has no coverage", |
| 30 | | usage: UnavailableOptions.usage), |
| 31 | | ] |
| 32 | | |
| 33 | 6 | private static var reportHelp: String { |
| 34 | 18 | let width = commands.map(\.word.count).max() ?? 0 |
| 35 | 18 | let list = commands.map { command in |
| 36 | 18 | let gap = String(repeating: " ", count: width - command.word.count + columnGap) |
| 37 | 18 | return " covane \(command.word)\(gap)\(command.summary)" |
| 38 | 18 | } |
| 39 | 6 | return """ |
| 40 | 6 | Write a static HTML coverage report from an llvm-cov export. |
| 41 | 6 | |
| 42 | 6 | \(ReportOptions.usage) |
| 43 | 6 | |
| 44 | 6 | Other commands: |
| 45 | 6 | \(list.joined(separator: "\n")) |
| 46 | 6 | |
| 47 | 6 | covane <command> --help shows a command's usage. |
| 48 | 6 | """ |
| 49 | 6 | } |
| 50 | | |
| 51 | | private static let columnGap = 3 |
| 52 | | |
| 53 | | /// Every option takes a value, so options sit at every other place. |
| 54 | 14 | private static func asksForHelp(options: ArraySlice<String>) -> Bool { |
| 55 | 14 | stride(from: options.startIndex, to: options.endIndex, by: 2).contains { index in |
| 56 | 14 | ["--help", "-h"].contains(options[index]) |
| 57 | 14 | } |
| 58 | 14 | } |
| 59 | | } |