Skip to content

Commit 95d5741

Browse files
committed
Add initializer to Positional for ArgumentGroup conforming types
feature/init-positional-with-argument-rep
1 parent b2660f3 commit 95d5741

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Sources/ArgumentEncoding/PositionalArgument.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,33 @@ extension Positional {
226226
}
227227
}
228228

229+
// MARK: Convenience initializers when Value: ArgumentGroup
230+
231+
extension Positional where Value: ArgumentGroup {
232+
/// Initializes a new positional when not used as a `@propertyWrapper`
233+
///
234+
/// - Parameters
235+
/// - wrappedValue: The underlying value
236+
public init(value: Value) {
237+
wrappedValue = value
238+
unwrap = Self.unwrap(_:)
239+
}
240+
241+
/// Initializes a new positional when used as a `@propertyWrapper`
242+
///
243+
/// - Parameters
244+
/// - wrappedValue: The underlying value
245+
public init(wrappedValue: Value) {
246+
self.wrappedValue = wrappedValue
247+
unwrap = Self.unwrap(_:)
248+
}
249+
250+
@Sendable
251+
public static func unwrap(_ value: Value) -> [String] {
252+
value.arguments()
253+
}
254+
}
255+
229256
// MARK: ExpressibleBy...Literal conformances
230257

231258
extension Positional: ExpressibleByIntegerLiteral where Value: BinaryInteger, Value.IntegerLiteralType == Int {

Tests/ArgumentEncodingTests/PositionalTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ final class PositionalTests: XCTestCase {
2828
let args = container.arguments()
2929
XCTAssertEqual(args, ["positional-argument"])
3030
}
31+
32+
func testPositionalArgumentGroup() throws {
33+
let positional =
34+
Positional(
35+
value: Container(configuration: RawValueCustomStringConvertible(rawValue: "positional-argument"))
36+
)
37+
let args = positional.arguments()
38+
XCTAssertEqual(args, ["positional-argument"])
39+
}
3140
}
3241

3342
private struct RawValueCustomStringConvertible: RawRepresentable, CustomStringConvertible {

0 commit comments

Comments
 (0)