Skip to content

Commit 8767280

Browse files
authored
CLI without ArgumentParser (#13)
* CLI without ArgumentParser * Add to changelog
1 parent b4619a3 commit 8767280

File tree

9 files changed

+234
-66
lines changed

9 files changed

+234
-66
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Un-released changes (main)
44

5+
* Reduce size of Tuist plugin archive to less than a third of before.
6+
57
## v0.0.1
68

79
* Initial release of SwiftHooks

Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ let package = Package(
1616
)
1717
],
1818
dependencies: [
19-
.package(url: "https://github.com/apple/swift-argument-parser", exact: "1.1.3"),
2019
.package(url: "https://github.com/JohnSundell/Files", exact: "4.2.0"),
2120
],
2221
targets: [
2322
.executableTarget(
2423
name: "SwiftHooksCLI",
2524
dependencies: [
26-
.target(name: "SwiftHooksKit"),
27-
.product(name: "ArgumentParser", package: "swift-argument-parser"),
25+
.target(name: "SwiftHooksKit")
2826
]
2927
),
3028
.target(
Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,56 @@
11
// Copyright © 2022 Andrew Lord.
22

3-
import ArgumentParser
43
import SwiftHooksKit
54

6-
struct InstallCommand: ParsableCommand {
7-
static var configuration = CommandConfiguration(
8-
commandName: "install",
9-
abstract: "Install shared Git hooks"
10-
)
5+
struct InstallCommand {
6+
let programName: String
7+
let option: String?
118

12-
@Flag(name: .shortAndLong, help: "Silence any output except errors")
13-
var quiet: Bool = false
9+
func run() {
10+
switch option {
11+
case .none:
12+
performInstall(isQuiet: false)
13+
case .some("-q"), .some("--quiet"):
14+
performInstall(isQuiet: true)
15+
case .some("-h"), .some("--help"):
16+
printHelp()
17+
case let .some(other):
18+
printUnexpectedOptionError(option: other)
19+
}
20+
}
1421

15-
func run() throws {
16-
SwiftHooks.configuration.printer = ConsolePrinter(quiet: quiet)
17-
try runCommand {
18-
try InstallHooksService()
19-
.run()
22+
private func performInstall(isQuiet: Bool) {
23+
SwiftHooks.configuration.printer = ConsolePrinter(quiet: isQuiet)
24+
runCommand {
25+
try InstallHooksService().run()
2026
}
2127
}
28+
29+
private func printUnexpectedOptionError(option: String) {
30+
let message = """
31+
Error: Unknown option '\(option)'
32+
33+
USAGE: \(programName) install [--quiet]
34+
35+
OPTIONS:
36+
-q, --quiet Silence any output except errors
37+
-h, --help Show help information.
38+
39+
"""
40+
print(message)
41+
}
42+
43+
private func printHelp() {
44+
let help = """
45+
OVERVIEW: Install shared Git hooks
46+
47+
USAGE: \(programName) install [--quiet]
48+
49+
OPTIONS:
50+
-q, --quiet Silence any output except errors
51+
-h, --help Show help information.
52+
53+
"""
54+
print(help)
55+
}
2256
}
Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,88 @@
11
// Copyright © 2022 Andrew Lord.
22

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+
}
1188
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright © 2022 Andrew Lord.
22

3-
import ArgumentParser
3+
import Darwin
44
import SwiftHooksKit
55

6-
extension ParsableCommand {
7-
func runCommand(runCommand: () throws -> Void) throws {
8-
do {
9-
try runCommand()
10-
} catch ExecutionError.failure {
11-
throw ExitCode.failure
12-
}
6+
func runCommand(runCommand: () throws -> Void) {
7+
do {
8+
try runCommand()
9+
} catch ExecutionError.failure {
10+
exit(EXIT_FAILURE)
11+
} catch {
12+
print(error)
13+
exit(EXIT_FAILURE)
1314
}
1415
}
Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,56 @@
11
// Copyright © 2022 Andrew Lord.
22

3-
import ArgumentParser
43
import SwiftHooksKit
54

6-
struct UninstallCommand: ParsableCommand {
7-
static var configuration = CommandConfiguration(
8-
commandName: "uninstall",
9-
abstract: "Uninstall shared Git hooks"
10-
)
5+
struct UninstallCommand {
6+
let programName: String
7+
let option: String?
118

12-
@Flag(name: .shortAndLong, help: "Silence any output except errors")
13-
var quiet: Bool = false
9+
func run() {
10+
switch option {
11+
case .none:
12+
performUninstall(isQuiet: false)
13+
case .some("-q"), .some("--quiet"):
14+
performUninstall(isQuiet: true)
15+
case .some("-h"), .some("--help"):
16+
printHelp()
17+
case let .some(other):
18+
printUnexpectedOptionError(option: other)
19+
}
20+
}
1421

15-
func run() throws {
16-
SwiftHooks.configuration.printer = ConsolePrinter(quiet: quiet)
17-
try runCommand {
18-
try UninstallHooksService()
19-
.run()
22+
private func performUninstall(isQuiet: Bool) {
23+
SwiftHooks.configuration.printer = ConsolePrinter(quiet: isQuiet)
24+
runCommand {
25+
try UninstallHooksService().run()
2026
}
2127
}
28+
29+
private func printUnexpectedOptionError(option: String) {
30+
let message = """
31+
Error: Unknown option '\(option)'
32+
33+
USAGE: \(programName) uninstall [--quiet]
34+
35+
OPTIONS:
36+
-q, --quiet Silence any output except errors
37+
-h, --help Show help information.
38+
39+
"""
40+
print(message)
41+
}
42+
43+
private func printHelp() {
44+
let help = """
45+
OVERVIEW: Uninstall shared Git hooks
46+
47+
USAGE: \(programName) uninstall [--quiet]
48+
49+
OPTIONS:
50+
-q, --quiet Silence any output except errors
51+
-h, --help Show help information.
52+
53+
"""
54+
print(help)
55+
}
2256
}
Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
11
// Copyright © 2022 Andrew Lord.
22

3-
import ArgumentParser
43
import SwiftHooksKit
54

6-
struct VersionCommand: ParsableCommand {
7-
static var configuration = CommandConfiguration(
8-
commandName: "version",
9-
abstract: "Print version"
10-
)
5+
struct VersionCommand {
6+
let programName: String
7+
let option: String?
118

12-
func run() throws {
9+
func run() {
10+
switch option {
11+
case .none:
12+
performVersion()
13+
case .some("-h"), .some("--help"):
14+
printHelp()
15+
case let .some(other):
16+
printUnexpectedOptionError(option: other)
17+
}
18+
}
19+
20+
private func performVersion() {
1321
SwiftHooks.configuration.printer = ConsolePrinter(quiet: false)
14-
VersionService()
15-
.run()
22+
VersionService().run()
23+
}
24+
25+
private func printUnexpectedOptionError(option: String) {
26+
let message = """
27+
Error: Unknown option '\(option)'
28+
29+
USAGE: \(programName) version
30+
31+
OPTIONS:
32+
-h, --help Show help information.
33+
34+
"""
35+
print(message)
36+
}
37+
38+
private func printHelp() {
39+
let help = """
40+
OVERVIEW: Print version
41+
42+
USAGE: \(programName) version
43+
44+
OPTIONS:
45+
-h, --help Show help information.
46+
47+
"""
48+
print(help)
1649
}
1750
}

Sources/SwiftHooksCLI/main.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
// Copyright © 2022 Andrew Lord.
22

3-
import Foundation
4-
5-
MainCommand.main()
3+
MainCommand().run()

0 commit comments

Comments
 (0)