Skip to content

Commit

Permalink
Fix AsyncParseableCommand hierarchy (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
KeithBird committed Jun 13, 2022
1 parent 48a799e commit c61fc0f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Expand Up @@ -73,4 +73,3 @@ extension AsyncMainProtocol {
}
}
}

33 changes: 32 additions & 1 deletion Sources/ArgumentParser/Parsable Types/ParsableCommand.swift
Expand Up @@ -84,7 +84,7 @@ extension ParsableCommand {
public static func helpMessage(
for subcommand: ParsableCommand.Type,
columns: Int? = nil
) -> String {
) -> String {
helpMessage(for: subcommand, includeHidden: false, columns: columns)
}

Expand Down Expand Up @@ -124,6 +124,13 @@ extension ParsableCommand {
/// - Parameter arguments: An array of arguments to use for parsing. If
/// `arguments` is `nil`, this uses the program's command-line arguments.
public static func main(_ arguments: [String]?) {

#if DEBUG
if #available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *) {
checkAsyncHierarchy(self, root: "\(self)")
}
#endif

do {
var command = try parseAsRoot(arguments)
try command.run()
Expand Down Expand Up @@ -164,4 +171,28 @@ extension ParsableCommand {
internal static var defaultIncludesUnconditionalArguments: Bool {
configuration.defaultSubcommand?.includesUnconditionalArguments == true
}

#if DEBUG
@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
internal static func checkAsyncHierarchy(_ command: ParsableCommand.Type, root: String) {
for sub in command.configuration.subcommands {
checkAsyncHierarchy(sub, root: root)

guard sub.configuration.subcommands.isEmpty else { continue }
guard sub is AsyncParsableCommand.Type else { continue }

fatalError("""
--------------------------------------------------------------------
Asynchronous subcommand of a synchronous root.
The asynchronous command `\(sub)` is declared as a subcommand of the synchronous root command `\(root)`.
With this configuration, your asynchronous `run()` method will not be called. To fix this issue, change `\(root)`'s `ParsableCommand` conformance to `AsyncParsableCommand`.
--------------------------------------------------------------------
""".wrapped(to: 70))
}
}
#endif
}

0 comments on commit c61fc0f

Please sign in to comment.