Skip to content

Commit b678ac0

Browse files
committed
Refactor argument processing code to be more in line with rest of SwiftPlate
- Use guard statement to validate array index. - Use initializer for Arguments instead of global function. - Fix bug where camel cased argument identifiers where compared against lowercased ones. - Update some variable names & code style.
1 parent 921721f commit b678ac0

File tree

1 file changed

+41
-54
lines changed

1 file changed

+41
-54
lines changed

main.swift

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,47 @@ extension FileManager {
5858
}
5959

6060
extension Array {
61-
func element(after index: UInt) -> Element? {
62-
63-
//: handles not only negative indices, but also index == self.count
64-
//: might be over-engineered, we only need to check for second condition
65-
let arrRange = [Int](0..<self.count)
66-
if arrRange.contains(Int(index) + 1) {
67-
return self[Int(index) + 1]
68-
} else {
61+
func element(after index: Int) -> Element? {
62+
guard index >= 0 && index < count else {
6963
return nil
7064
}
65+
66+
return self[index + 1]
7167
}
7268
}
7369

7470
// MARK: - Types
7571

72+
struct Arguments {
73+
var destination: String?
74+
var projectName: String?
75+
var authorName: String?
76+
var authorEmail: String?
77+
var githubURL: String?
78+
var organizationName: String?
79+
80+
init(commandLineArguments arguments: [String]) {
81+
for (index, argument) in arguments.enumerated() {
82+
switch argument.lowercased() {
83+
case "--destination":
84+
destination = arguments.element(after: index)
85+
case "--projectname":
86+
projectName = arguments.element(after: index)
87+
case "--authorname":
88+
authorName = arguments.element(after: index)
89+
case "--authoremail":
90+
authorEmail = arguments.element(after: index)
91+
case "--githuburl":
92+
githubURL = arguments.element(after: index)
93+
case "--organizationname":
94+
organizationName = arguments.element(after: index)
95+
default:
96+
break
97+
}
98+
}
99+
}
100+
}
101+
76102
class StringReplacer {
77103
private let projectName: String
78104
private let authorName: String
@@ -200,56 +226,17 @@ func performCommand(description: String, command: () throws -> Void) rethrows {
200226
print("✅ Done")
201227
}
202228

203-
//: This *could* just be a dictionary, but a struct felt cleaner. Also, we get type safety(and autocompletion) for free!!
204-
struct Arguments {
205-
var destination: String?
206-
var projectName: String?
207-
var authorName: String?
208-
var authorEmail: String?
209-
var githubURL: String?
210-
var organizationName: String?
211-
}
212-
213-
func processArguments() -> Arguments {
214-
var templateInfo = Arguments()
215-
216-
let args = CommandLine.arguments
217-
218-
for (index,argument) in args.enumerated() {
219-
//: Why lowercased() ? In case the user accidentally mistypes something.
220-
switch argument.lowercased() {
221-
case "--destination" :
222-
templateInfo.destination = args.element(after: UInt(index))
223-
case "--projectName" :
224-
templateInfo.projectName = args.element(after: UInt(index))
225-
case "--authorName" :
226-
templateInfo.authorName = args.element(after: UInt(index))
227-
case "--authorEmail" :
228-
templateInfo.authorEmail = args.element(after: UInt(index))
229-
case "--githubURL" :
230-
templateInfo.githubURL = args.element(after: UInt(index))
231-
case "--organizationName" :
232-
templateInfo.organizationName = args.element(after: UInt(index))
233-
default:
234-
//: We should probably put something to skip an argument value, but for now let's just continue on the loop ¯\_(ツ)_/¯
235-
break
236-
}
237-
}
238-
return templateInfo
239-
}
240-
241229
// MARK: - Program
242230

243231
print("Welcome to the SwiftPlate project generator 🐣")
244232

245-
let templateFromArguments = processArguments()
246-
247-
let destination = templateFromArguments.destination ?? askForDestination()
248-
let projectName = templateFromArguments.projectName ?? askForRequiredInfo(question: "📛 What's the name of your project?", errorMessage: "Project name cannot be empty")
249-
let authorName = templateFromArguments.authorName ?? askForRequiredInfo(question: "👶 What's your name?", errorMessage: "Your name cannot be empty")
250-
let authorEmail = templateFromArguments.authorEmail ?? askForOptionalInfo(question: "📫 What's your email address (for Podspec)?")
251-
let gitHubURL = templateFromArguments.githubURL ?? askForOptionalInfo(question: "🌍 Any GitHub URL that you'll be hosting this project at (for Podspec)?")
252-
let organizationName = templateFromArguments.organizationName ?? askForOptionalInfo(question: "🏢 What's your organization name?")
233+
let arguments = Arguments(commandLineArguments: CommandLine.arguments)
234+
let destination = arguments.destination ?? askForDestination()
235+
let projectName = arguments.projectName ?? askForRequiredInfo(question: "📛 What's the name of your project?", errorMessage: "Project name cannot be empty")
236+
let authorName = arguments.authorName ?? askForRequiredInfo(question: "👶 What's your name?", errorMessage: "Your name cannot be empty")
237+
let authorEmail = arguments.authorEmail ?? askForOptionalInfo(question: "📫 What's your email address (for Podspec)?")
238+
let gitHubURL = arguments.githubURL ?? askForOptionalInfo(question: "🌍 Any GitHub URL that you'll be hosting this project at (for Podspec)?")
239+
let organizationName = arguments.organizationName ?? askForOptionalInfo(question: "🏢 What's your organization name?")
253240

254241
print("---------------------------------------------------------------------")
255242
print("SwiftPlate will now generate a project with the following parameters:")

0 commit comments

Comments
 (0)