Skip to content

Commit 4ea9880

Browse files
committed
Add value formatting to OptionFormatter
feature/option-value-formatting
1 parent f5a0682 commit 4ea9880

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Sources/ArgumentEncoding/Formatters.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public struct OptionFormatter: Sendable {
5656
public let prefix: @Sendable () -> String
5757
public let body: @Sendable (_ key: String) -> String
5858
public let separator: @Sendable () -> String
59+
public let value: @Sendable (_ value: String) -> String
5960

6061
public func format(key: String, value: String) -> String {
61-
prefix() + body(key) + separator() + value
62+
prefix() + body(key) + separator() + self.value(value)
6263
}
6364

6465
func format(encoding: OptionEncoding) -> String {
@@ -71,14 +72,17 @@ public struct OptionFormatter: Sendable {
7172
/// - prefix: Closure that returns the prefix string
7273
/// - body: Closure that transforms the key string for formatting
7374
/// - separator: Closure that returns the string that separates the key and value
75+
/// - value: Closure that transforms the value string for formatting
7476
public init(
7577
prefix: @escaping @Sendable () -> String,
7678
body: @escaping @Sendable (_ key: String) -> String,
77-
separator: @escaping @Sendable () -> String
79+
separator: @escaping @Sendable () -> String,
80+
value: @escaping @Sendable (_ value: String) -> String
7881
) {
7982
self.prefix = prefix
8083
self.body = body
8184
self.separator = separator
85+
self.value = value
8286
}
8387

8488
/// Initialize a new formatter
@@ -87,15 +91,18 @@ public struct OptionFormatter: Sendable {
8791
/// - prefix: Name spaced closure that returns the prefix string for a Flag
8892
/// - body: Name spaced closure that transforms the key string for formatting
8993
/// - separator: Name spaced closure that returns the string that separates the key and value
94+
/// - value: Name spaced closure that transforms the value string for formatting
9095
public init(
9196
prefix: PrefixFormatter = .empty,
9297
body: BodyFormatter = .empty,
93-
separator: SeparatorFormatter = .space
98+
separator: SeparatorFormatter = .space,
99+
value: BodyFormatter = .empty
94100
) {
95101
self.init(
96102
prefix: prefix.transform,
97103
body: body.transform,
98-
separator: separator.transform
104+
separator: separator.transform,
105+
value: value.transform
99106
)
100107
}
101108
}
@@ -126,6 +133,7 @@ public struct BodyFormatter: Sendable {
126133
public static let empty = Self { $0 }
127134
public static let kebabCase = Self(CaseConverter.kebabCase)
128135
public static let snakeCase = Self(CaseConverter.snakeCase)
136+
public static let singleQuote = Self { "'\($0)'" }
129137
}
130138

131139
/// Name space for a closure that returns the separator string between an Option's key and value

Tests/ArgumentEncodingTests/FormatterTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,11 @@ final class FormatterTests: XCTestCase {
8787
"optionKey=optionValue"
8888
)
8989
}
90+
91+
func testOptionFormatterSingleQuoteValue() throws {
92+
XCTAssertEqual(
93+
OptionFormatter(value: .singleQuote).format(key: "optionKey", value: "optionValue"),
94+
"optionKey 'optionValue'"
95+
)
96+
}
9097
}

0 commit comments

Comments
 (0)