|
1 | 1 | // Copyright © 2022 Andrew Lord. |
2 | 2 |
|
3 | | -import ArgumentParser |
4 | | - |
5 | | -struct MainCommand: ParsableCommand { |
6 | | - static var configuration = CommandConfiguration( |
7 | | - commandName: "hooks", |
8 | | - abstract: "", |
9 | | - subcommands: [InstallCommand.self, UninstallCommand.self, VersionCommand.self] |
10 | | - ) |
| 3 | +import Darwin |
| 4 | +import Foundation |
| 5 | + |
| 6 | +struct MainCommand { |
| 7 | + func run() { |
| 8 | + let programName = extractProgramName() |
| 9 | + let (subcommand, option) = extractSubcommand(programName: programName) |
| 10 | + switch subcommand { |
| 11 | + case "install": |
| 12 | + InstallCommand(programName: programName, option: option).run() |
| 13 | + case "uninstall": |
| 14 | + UninstallCommand(programName: programName, option: option).run() |
| 15 | + case "version": |
| 16 | + VersionCommand(programName: programName, option: option).run() |
| 17 | + case "-h", "--help": |
| 18 | + printHelp(programName: programName) |
| 19 | + default: |
| 20 | + printUnexpectedArgumentError(programName: programName, argument: subcommand) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + private func extractProgramName() -> String { |
| 25 | + guard let program = CommandLine.arguments.first else { |
| 26 | + exit(EXIT_SUCCESS) |
| 27 | + } |
| 28 | + var programName = URL(fileURLWithPath: program).lastPathComponent |
| 29 | + if programName == "tuist-hooks" { |
| 30 | + programName = "tuist hooks" |
| 31 | + } |
| 32 | + return programName |
| 33 | + } |
| 34 | + |
| 35 | + private func extractSubcommand(programName: String) -> (subcommand: String, option: String?) { |
| 36 | + let arguments = Array(CommandLine.arguments.dropFirst()) |
| 37 | + guard arguments.count > 0 else { |
| 38 | + printHelp(programName: programName) |
| 39 | + exit(EXIT_SUCCESS) |
| 40 | + } |
| 41 | + guard arguments.count <= 2, let command = arguments.first else { |
| 42 | + printHelp(programName: programName) |
| 43 | + exit(EXIT_FAILURE) |
| 44 | + } |
| 45 | + if arguments.count == 1 { |
| 46 | + return (subcommand: command, option: nil) |
| 47 | + } |
| 48 | + return (subcommand: command, option: arguments.last) |
| 49 | + } |
| 50 | + |
| 51 | + private func printUnexpectedArgumentError(programName: String, argument: String) { |
| 52 | + let message = """ |
| 53 | + Error: Unknown argument '\(argument)' |
| 54 | +
|
| 55 | + USAGE: \(programName) <subcommand> |
| 56 | +
|
| 57 | + OPTIONS: |
| 58 | + -h, --help Show help information. |
| 59 | +
|
| 60 | + SUBCOMMANDS: |
| 61 | + install Install shared Git hooks |
| 62 | + uninstall Uninstall shared Git hooks |
| 63 | + version Print version |
| 64 | +
|
| 65 | + See '\(programName) <subcommand> --help' for detailed help. |
| 66 | +
|
| 67 | + """ |
| 68 | + print(message) |
| 69 | + } |
| 70 | + |
| 71 | + private func printHelp(programName: String) { |
| 72 | + let help = """ |
| 73 | + USAGE: \(programName) <subcommand> |
| 74 | +
|
| 75 | + OPTIONS: |
| 76 | + -h, --help Show help information. |
| 77 | +
|
| 78 | + SUBCOMMANDS: |
| 79 | + install Install shared Git hooks |
| 80 | + uninstall Uninstall shared Git hooks |
| 81 | + version Print version |
| 82 | +
|
| 83 | + See '\(programName) <subcommand> --help' for detailed help. |
| 84 | +
|
| 85 | + """ |
| 86 | + print(help) |
| 87 | + } |
11 | 88 | } |
0 commit comments