Skip to content
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
3 changes: 2 additions & 1 deletion Sources/ArgumentParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ add_library(ArgumentParser
Utilities/StringExtensions.swift
Utilities/SwiftExtensions.swift
Utilities/Tree.swift


Validators/AsyncCompletionsValidator.swift
Validators/CodingKeyValidator.swift
Validators/NonsenseFlagsValidator.swift
Validators/ParsableArgumentsValidation.swift
Expand Down
15 changes: 0 additions & 15 deletions Sources/ArgumentParser/Parsable Properties/CompletionKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ public struct CompletionKind {
case directory
case shellCommand(String)
case custom(@Sendable ([String], Int, String) -> [String])
#if !canImport(Dispatch)
@available(*, unavailable, message: "DispatchSemaphore is unavailable")
case customAsync(@Sendable ([String], Int, String) async -> [String])
#else
case customAsync(@Sendable ([String], Int, String) async -> [String])
#endif
case customDeprecated(@Sendable ([String]) -> [String])
}

Expand Down Expand Up @@ -186,22 +181,12 @@ public struct CompletionKind {
///
/// The same as `custom(@Sendable @escaping ([String], Int, String) -> [String])`,
/// except that the closure is asynchronous.
#if !canImport(Dispatch)
@available(*, unavailable, message: "DispatchSemaphore is unavailable")
@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
public static func custom(
_ completion: @Sendable @escaping ([String], Int, String) async -> [String]
) -> CompletionKind {
fatalError("DispatchSemaphore is unavailable")
}
#else
@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
public static func custom(
_ completion: @Sendable @escaping ([String], Int, String) async -> [String]
) -> CompletionKind {
CompletionKind(kind: .customAsync(completion))
}
#endif

/// Deprecated; only kept for backwards compatibility.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ public protocol AsyncParsableCommand: ParsableCommand {

@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
extension AsyncParsableCommand {
/// Parses a new instance of this type from command-line arguments.
///
/// - Parameter arguments: An array of arguments to use for parsing. If
/// `arguments` is `nil`, this uses the program's command-line arguments.
/// - Returns: A new instance of this type.
/// - Throws: If parsing failed or arguments contains a help request.
public static func parse(
_ arguments: [String]? = nil
) async throws -> Self {
try parse(try await parseAsRoot(arguments))
}

/// Parses an instance of this type, or one of its subcommands, from
/// command-line arguments.
///
/// - Parameter arguments: An array of arguments to use for parsing. If
/// `arguments` is `nil`, this uses the program's command-line arguments.
/// - Returns: A new instance of this type, one of its subcommands, or a
/// command type internal to the `ArgumentParser` library.
/// - Throws: If parsing fails.
public static func parseAsRoot(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking the public API.

@natecook1000 / @rgoldberg This is failing builds

FooBarTests.swift:39:17: error: expression is 'async' but is not marked with 'await'
37 |         for invalid in invalids {
38 |             await XCTAssertThrowsError(
39 |                 try FooBar.parse([
   |                 |   `- note: call is 'async' in an autoclosure argument
   |                 `- error: expression is 'async' but is not marked with 'await'
40 |                     invalid,
41 |                 ]),

_ arguments: [String]? = nil
) async throws -> ParsableCommand {
var parser = CommandParser(self)
let arguments = arguments ?? Array(CommandLine._staticArguments.dropFirst())
return try await parser.parse(arguments: arguments)
}

/// Executes this command, or one of its subcommands, with the given arguments.
///
/// This method parses an instance of this type, one of its subcommands, or
Expand All @@ -36,7 +64,7 @@ extension AsyncParsableCommand {
/// `arguments` is `nil`, this uses the program's command-line arguments.
public static func main(_ arguments: [String]?) async {
do {
var command = try parseAsRoot(arguments)
var command = try await parseAsRoot(arguments)
if var asyncCommand = command as? AsyncParsableCommand {
try await asyncCommand.run()
} else {
Expand Down
10 changes: 8 additions & 2 deletions Sources/ArgumentParser/Parsable Types/ParsableArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public protocol ParsableArguments: Decodable, _SendableMetatype {

/// The label to use for "Error: ..." messages from this type (experimental).
///
/// Can be ignored if `_errorPrefix`'s is changed.
/// `_errorLabel` will be ignored if `_errorPrefix` is changed to ignore `_errorLabel`.
@available(*, deprecated, message: "Use _errorPrefix instead.")
static var _errorLabel: String { get }

Expand Down Expand Up @@ -88,9 +88,15 @@ extension ParsableArguments {
/// - Throws: If parsing failed or arguments contains a help request.
public static func parse(
_ arguments: [String]? = nil
) throws -> Self {
try parse(try self.asCommand.parseAsRoot(arguments))
}

internal static func parse(
_ command: ParsableCommand
) throws -> Self {
// Parse the command and unwrap the result if necessary.
switch try self.asCommand.parseAsRoot(arguments) {
switch command {
case let helpCommand as HelpCommand:
throw ParserError.helpRequested(visibility: helpCommand.visibility)
case let result as _WrappedParsableCommand<Self>:
Expand Down
Loading
Loading