Skip to content

Commit e7f312e

Browse files
authored
Cleanup Foundation usage (#528)
- Changes uses of import Foundation in ArgumentParser to only expose the symbols needed to avoid growing unintentional dependencies. - Replaces direct usage of EXIT_FAILURE in MessageInfo with ExitCode, exposed as a result of the above change.
1 parent e48e467 commit e7f312e

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

Plugins/GenerateManualPlugin/GenerateManualPlugin.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
import PackagePlugin
13-
import Foundation
1413

1514
@main
1615
struct GenerateManualPlugin: CommandPlugin {

Sources/ArgumentParser/Usage/DumpHelpGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
@_implementationOnly import Foundation
1312
@_implementationOnly import ArgumentParserToolInfo
13+
@_implementationOnly import class Foundation.JSONEncoder
1414

1515
internal struct DumpHelpGenerator {
1616
var toolInfo: ToolInfoV0

Sources/ArgumentParser/Usage/MessageInfo.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
@_implementationOnly import Foundation
12+
@_implementationOnly import protocol Foundation.LocalizedError
13+
@_implementationOnly import class Foundation.NSError
1314

1415
enum MessageInfo {
1516
case help(text: String)
1617
case validation(message: String, usage: String, help: String)
17-
case other(message: String, exitCode: Int32)
18+
case other(message: String, exitCode: ExitCode)
1819

1920
init(error: Error, type: ParsableArguments.Type) {
2021
var commandStack: [ParsableCommand.Type]
@@ -105,15 +106,15 @@ enum MessageInfo {
105106
case .message(let message):
106107
self = .help(text: message)
107108
}
108-
case let error as ExitCode:
109-
self = .other(message: "", exitCode: error.rawValue)
109+
case let exitCode as ExitCode:
110+
self = .other(message: "", exitCode: exitCode)
110111
case let error as LocalizedError where error.errorDescription != nil:
111-
self = .other(message: error.errorDescription!, exitCode: EXIT_FAILURE)
112+
self = .other(message: error.errorDescription!, exitCode: .failure)
112113
default:
113114
if Swift.type(of: error) is NSError.Type {
114-
self = .other(message: error.localizedDescription, exitCode: EXIT_FAILURE)
115+
self = .other(message: error.localizedDescription, exitCode: .failure)
115116
} else {
116-
self = .other(message: String(describing: error), exitCode: EXIT_FAILURE)
117+
self = .other(message: String(describing: error), exitCode: .failure)
117118
}
118119
}
119120
} else if let parserError = parserError {
@@ -126,7 +127,7 @@ enum MessageInfo {
126127
let helpAbstract = argumentSet.helpDescription(error: parserError) ?? ""
127128
self = .validation(message: message, usage: usage, help: helpAbstract)
128129
} else {
129-
self = .other(message: String(describing: error), exitCode: EXIT_FAILURE)
130+
self = .other(message: String(describing: error), exitCode: .failure)
130131
}
131132
}
132133

@@ -165,7 +166,7 @@ enum MessageInfo {
165166
switch self {
166167
case .help: return ExitCode.success
167168
case .validation: return ExitCode.validationFailure
168-
case .other(_, let code): return ExitCode(code)
169+
case .other(_, let exitCode): return exitCode
169170
}
170171
}
171172
}

Sources/ArgumentParser/Usage/UsageGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
@_implementationOnly import Foundation
12+
@_implementationOnly import protocol Foundation.LocalizedError
1313

1414
struct UsageGenerator {
1515
var toolName: String

0 commit comments

Comments
 (0)