Skip to content

Initialize Positional with a type that conforms to ArgumentGroup #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sources/ArgumentEncoding/PositionalArgument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@ extension Positional {
}
}

// MARK: Convenience initializers when Value: ArgumentGroup

extension Positional where Value: ArgumentGroup {
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(value: Value) {
wrappedValue = value
unwrap = Self.unwrap(_:)
}

/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
unwrap = Self.unwrap(_:)
}

@Sendable
public static func unwrap(_ value: Value) -> [String] {
value.arguments()
}
}

// MARK: ExpressibleBy...Literal conformances

extension Positional: ExpressibleByIntegerLiteral where Value: BinaryInteger, Value.IntegerLiteralType == Int {
Expand Down
9 changes: 9 additions & 0 deletions Tests/ArgumentEncodingTests/PositionalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ final class PositionalTests: XCTestCase {
let args = container.arguments()
XCTAssertEqual(args, ["positional-argument"])
}

func testPositionalArgumentGroup() throws {
let positional =
Positional(
value: Container(configuration: RawValueCustomStringConvertible(rawValue: "positional-argument"))
)
let args = positional.arguments()
XCTAssertEqual(args, ["positional-argument"])
}
}

private struct RawValueCustomStringConvertible: RawRepresentable, CustomStringConvertible {
Expand Down