Skip to content

Commit 74686c3

Browse files
authored
Merge pull request #15 from MFB-Technologies-Inc/feature/init-positional-with-argument-rep
Initialize Positional with a type that conforms to ArgumentGroup
2 parents 0ec6b15 + f84d612 commit 74686c3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Sources/ArgumentEncoding/PositionalArgument.swift

+27
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,33 @@ extension Positional {
224224
}
225225
}
226226

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

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

Tests/ArgumentEncodingTests/PositionalTests.swift

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ final class PositionalTests: XCTestCase {
2626
let args = container.arguments()
2727
XCTAssertEqual(args, ["positional-argument"])
2828
}
29+
30+
func testPositionalArgumentGroup() throws {
31+
let positional =
32+
Positional(
33+
value: Container(configuration: RawValueCustomStringConvertible(rawValue: "positional-argument"))
34+
)
35+
let args = positional.arguments()
36+
XCTAssertEqual(args, ["positional-argument"])
37+
}
2938
}
3039

3140
private struct RawValueCustomStringConvertible: RawRepresentable, CustomStringConvertible {

0 commit comments

Comments
 (0)