1/// What `--help` and `-h` print, for `covane` and each of its commands.
2public 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`).
614 public static func text(for arguments: [String]) -> String? {
737 let command = commands.first { $0.word == arguments.first }
814 guard asksForHelp(options: arguments.dropFirst(command == nil ? 0 : 1)) else {
95 return nil
109 }
119 guard let command else { return reportHelp }
123 return "\(command.summary).\n\n\(command.usage)"
1314 }
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
336 private static var reportHelp: String {
3418 let width = commands.map(\.word.count).max() ?? 0
3518 let list = commands.map { command in
3618 let gap = String(repeating: " ", count: width - command.word.count + columnGap)
3718 return " covane \(command.word)\(gap)\(command.summary)"
3818 }
396 return """
406 Write a static HTML coverage report from an llvm-cov export.
416
426 \(ReportOptions.usage)
436
446 Other commands:
456 \(list.joined(separator: "\n"))
466
476 covane <command> --help shows a command's usage.
486 """
496 }
50
51 private static let columnGap = 3
52
53 /// Every option takes a value, so options sit at every other place.
5414 private static func asksForHelp(options: ArraySlice<String>) -> Bool {
5514 stride(from: options.startIndex, to: options.endIndex, by: 2).contains { index in
5614 ["--help", "-h"].contains(options[index])
5714 }
5814 }
59}