-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.swift
executable file
·286 lines (251 loc) · 8.16 KB
/
template.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/swift
////////////////////////////////////////////////////////////////////////////////
// Script Name: template.swift
// Written by: John W. Woolsey
// Copyright © 2015. All rights reserved.
// Description:
// Swift shell script template with command line interface.
// Please see the printHelp function for syntax information.
////////////////////////////////////////////////////////////////////////////////
import Foundation
#if os(macOS)
import Darwin
#elseif os(Linux)
import Glibc
#endif
// MARK: - Enumerations
enum ConsoleOutputType {
case standard
case debug
case warning
case error
}
// MARK: - Properties
// Program information
var outputDateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
return formatter
}
let programPath = CommandLine.arguments[0]
let programName = (programPath as NSString).lastPathComponent
var programVersion = "$Revision: 1.5 $" // Changed automatically by RCS
var programDate: String {
if let fileAttributes = try? FileManager.default.attributesOfItem(atPath: programPath),
let fileModificationDate = fileAttributes[FileAttributeKey.modificationDate] as? Date {
return outputDateFormatter.string(from: fileModificationDate)
} else {
return ""
}
}
var numberOfErrors = 0
// Command line options
var debugOption = false
var helpOption = false
var versionOption = false
var outputFileName:String?
// MARK: - Functions
/// Initializes program.
func initProgram() {
let programVersionComponents = programVersion.components(separatedBy: " ")
if programVersionComponents.count > 1 {
programVersion = programVersionComponents[1]
}
}
/// Prints program version.
func printVersion() {
print("\(programName) \(programVersion) \(programDate)")
}
/// Prints program help message.
func printHelp() {
printVersion()
// MARK: Program Syntax
print("")
print("Description:")
print(" Swift based command line program template.\n")
print("Syntax: \(programName) [ -d ] -h | -v | [ -o <outfile> ] <infile>\n")
print("Where:")
print(" <infile> represents the input text (.txt) file(s).")
print(" <outfile> represents the output text file.\n")
print("Options:")
print(" -d specify debug mode.")
print(" -h print program help.")
print(" -o <outfile> specify output file name.")
print(" -v print program version.\n")
print("Example Invocation:")
print(" % \(programName) input.txt")
print(" % \(programName) -o ofile.txt ifile.txt")
}
/// Finalizes and ends program.
func endProgram() {
if debugOption {
writeMessage("Program ended with exit code of '\(numberOfErrors)'.", to: .debug)
}
exit(Int32(numberOfErrors))
}
/// Gets command line options.
///
/// - Returns: The number of processed command line arguments.
func getOptions() -> Int {
var argumentsProcessedCount = 1
let pattern = "dho:v"
let buffer = Array(pattern.utf8).map { Int8($0) }
while true {
let option = Int(getopt(CommandLine.argc, CommandLine.unsafeArgv, buffer))
if option == -1 { break } // no more options available
argumentsProcessedCount += 1
switch "\(UnicodeScalar(UInt8(option)))" {
case "d": // debug mode
debugOption = true
case "h": // print help message
helpOption = true
case "o": // output file specified
outputFileName = String(cString: optarg)
argumentsProcessedCount += 1
if let fileName = outputFileName {
if fileName.hasPrefix("-") {
writeMessage("Missing output file name.", to: .error)
numberOfErrors += 1
printHelp()
endProgram()
}
} else {
writeMessage("Could not retrieve output file name.", to: .error)
numberOfErrors += 1
printHelp()
endProgram()
}
case "v": // print version message
versionOption = true
default:
writeMessage("Could not retrieve an argument.", to: .error)
numberOfErrors += 1
printHelp()
endProgram()
}
}
return argumentsProcessedCount
}
/// Processes command line options.
func processOptions() {
if debugOption {
writeMessage("Running in debug mode.", to: .debug)
writeMessage("Arguments are \(CommandLine.arguments).", to: .debug)
if let fileName = outputFileName {
writeMessage("Output file name is '\(fileName)'.", to: .debug)
}
}
if helpOption {
printHelp()
endProgram()
} else if versionOption {
printVersion()
endProgram()
}
}
/// Processes input files.
///
/// - Parameter inputFileNames: The input file names.
/// - Returns: The result of processing the input files.
func processFiles(_ inputFileNames:[String]) -> String {
var results = ""
let fileManager = FileManager.default
for fileName in inputFileNames {
if debugOption {
writeMessage("Processing file '\(fileName)'.", to: .debug)
}
if !fileName.lowercased().hasSuffix(".txt") || !fileManager.fileExists(atPath: fileName) {
writeMessage("Could not read from file '\(fileName)'.", to: .error)
numberOfErrors += 1
continue
}
do {
let fileContents = try String(contentsOfFile: fileName, encoding: String.Encoding.utf8)
for row in fileContents.components(separatedBy: "\n") { // rows separated by new lines
if row.count > 0 {
// TODO: Do file processing here
results += programName + ": " + row + "\n"
}
}
} catch let error {
writeMessage("Could not read from file '\(fileName)'. \(error.localizedDescription)", to: .error)
numberOfErrors += 1
}
}
return results
}
/// Prints results.
///
/// - Parameter results: The contents to print.
func printResults(_ results:String) {
if results.count > 0 {
if let fileName = outputFileName {
do {
try results.write(toFile: fileName, atomically: false, encoding: String.Encoding.utf8)
} catch let error {
writeMessage("Could not write to file '\(fileName)'. \(error.localizedDescription)", to: .error)
numberOfErrors += 1
}
} else {
print(results)
}
}
}
/// Writes a message to the console.
///
/// Messages with .debug, .warning, and .error types are prepended with the associated type.
///
/// The .standard and .debug types are sent to STDOUT, whereas .warning and .error types are sent to STDERR.
///
/// - Parameters:
/// - message: The message to write.
/// - to: The message type. One of .standard (default), .debug, .warning, or .error must be used.
func writeMessage(_ message: String, to: ConsoleOutputType = .standard) {
switch to {
case .standard:
print("\(programName): \(message)")
case .debug:
print("\(programName): DEBUG - \(message)")
case .warning:
fputs("\(programName): Warning - \(message)\n", stderr)
case .error:
fputs("\(programName): ERROR - \(message)\n", stderr)
}
}
// MARK: - Main Section
initProgram()
let argumentsProcessedCount = getOptions()
processOptions()
if CommandLine.arguments.count <= argumentsProcessedCount { // check for file arguments
writeMessage("Missing command line arguments.", to: .error)
numberOfErrors += 1
printHelp()
endProgram()
}
var inputFileNames = [String]()
for argument in CommandLine.arguments[argumentsProcessedCount..<CommandLine.arguments.count] {
inputFileNames.append(argument)
}
let results = processFiles(inputFileNames)
printResults(results)
endProgram()
// MARK: - Revision History
////////////////////////////////////////////////////////////////////////////////
// Revision History
//
// $Log: template.swift,v $
// Revision 1.5 2019/05/09 01:31:46 woolsey
// Changed OSX to macOS in compiler directive.
//
// Revision 1.4 2019/05/08 21:42:14 woolsey
// Updated to Swift 5.0 compatibility.
//
// Revision 1.3 2016/12/04 16:03:03 woolsey
// Migrated to Swift 3.0.
//
// Revision 1.2 2016/06/10 15:19:05 woolsey
// Updated to Swift 2.2 syntax.
//
// Revision 1.1 2015/02/23 19:35:39 woolsey
// Initial revision
//