Skip to content

Commit c3c1a5f

Browse files
authored
Add an experimental customization point for the error label (#223)
SwiftPM uses "error:" instead of "Error:" to make its error messages match the compiler.
1 parent 5323d56 commit c3c1a5f

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

Sources/ArgumentParser/Parsable Types/CommandConfiguration.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,3 @@ public struct CommandConfiguration {
110110
self.helpNames = helpNames
111111
}
112112
}
113-
114-

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public protocol ParsableArguments: Decodable {
3434
/// Implement this method to perform validation or other processing after
3535
/// creating a new instance from command-line arguments.
3636
mutating func validate() throws
37+
38+
/// The label to use for "Error: ..." messages from this type. (experimental)
39+
static var _errorLabel: String { get }
3740
}
3841

3942
/// A type that provides the `ParsableCommand` interface to a `ParsableArguments` type.
@@ -71,6 +74,10 @@ extension ParsableArguments {
7174
internal static var asCommand: ParsableCommand.Type {
7275
self as? ParsableCommand.Type ?? _WrappedParsableCommand<Self>.self
7376
}
77+
78+
public static var _errorLabel: String {
79+
"Error"
80+
}
7481
}
7582

7683
// MARK: - API
@@ -121,7 +128,7 @@ extension ParsableArguments {
121128
public static func fullMessage(
122129
for error: Error
123130
) -> String {
124-
MessageInfo(error: error, type: self).fullText
131+
MessageInfo(error: error, type: self).fullText(for: self)
125132
}
126133

127134
/// Returns the text of the help screen for this type.
@@ -174,11 +181,12 @@ extension ParsableArguments {
174181
}
175182

176183
let messageInfo = MessageInfo(error: error, type: self)
177-
if !messageInfo.fullText.isEmpty {
184+
let fullText = messageInfo.fullText(for: self)
185+
if !fullText.isEmpty {
178186
if messageInfo.shouldExitCleanly {
179-
print(messageInfo.fullText)
187+
print(fullText)
180188
} else {
181-
print(messageInfo.fullText, to: &standardError)
189+
print(fullText, to: &standardError)
182190
}
183191
}
184192
_exit(messageInfo.exitCode.rawValue)

Sources/ArgumentParser/Usage/MessageInfo.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ enum MessageInfo {
120120
}
121121
}
122122

123-
var fullText: String {
123+
func fullText(for args: ParsableArguments.Type) -> String {
124124
switch self {
125125
case .help(text: let text):
126126
return text
127127
case .validation(message: let message, usage: let usage):
128-
let errorMessage = message.isEmpty ? "" : "Error: \(message)\n"
128+
let errorMessage = message.isEmpty ? "" : "\(args._errorLabel): \(message)\n"
129129
return errorMessage + usage
130130
case .other(let message, _):
131-
return message.isEmpty ? "" : "Error: \(message)"
131+
return message.isEmpty ? "" : "\(args._errorLabel): \(message)"
132132
}
133133
}
134134

0 commit comments

Comments
 (0)